How to print object as json/xml in Log.Message

Started by LeatherCat, December 06, 2019, 05:30:15 PM

Previous topic - Next topic

LeatherCat

Hey!
In general - I need to print some objects using Log.Message. How does one do that in rimworld project?

I tried to serialize with .NET System.Runtime.Serialization and JavaScriptSerializer.
I've added required .NET dlls into Assemblies where is my mod's dll built. These dlls are from .NET 2.0 and 3.5
Mod is built fine.

But there is an error when game is loaded:
ReflectionTypeLoadException getting types in assembly System.Runtime.Serialization: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)


I joined discord  - can't type in any channel yet...

LeatherCat

Also I tried to add these system dlls into game's Managed dir with other dlls

LeatherCat

UI log panel has hard time if dump big objects using Log.Message
Need to find a way write to filesystem from RW.

Currently I use brutal dump util methods:
public static string DisplayObjectInfo(Object o) {
            return DisplayObjectInfo(new StringBuilder(), o, 0);
        }

        public static string DisplayObjectInfo(StringBuilder sb, Object obj, int level) {
            if (obj is ICollection) {
                ICollection collection = (ICollection)obj;
                if (collection.Count == 0) {
                    return "-empty collection-";
                }

                int count = 0;
                foreach (Object obj2 in collection) {
                    count++;
                    sb.Append("\r\n\r\n" + getLevelPrefix(level) + "OBJECT #" + count + "\r\n");
                    sb.Append(DisplayObjectInfo(sb, obj2, level + 1));
                }
                return sb.ToString();
            }

            System.Type type = obj.GetType();
            sb.Append(getLevelPrefix(level) + "Type: " + type.Name);

            sb.Append("\r\n\r\n"+ getLevelPrefix(level) +"Fields:");
            System.Reflection.FieldInfo[] fi = type.GetFields();
            if (fi.Length > 0) {
                foreach (FieldInfo f in fi) {
                    sb.Append("\r\n" + getLevelPrefix(level) + f.ToString() + " = " + f.GetValue(obj));
                }
            } else
                sb.Append("\r\n"+ getLevelPrefix(level) + "None");

            sb.Append("\r\n\r\n"+ getLevelPrefix(level) + "Properties:");
            System.Reflection.PropertyInfo[] pi = type.GetProperties();
            if (pi.Length > 0) {
                foreach (PropertyInfo p in pi) {
                    sb.Append("\r\n" + getLevelPrefix(level) + p.ToString() + " = " +
                              p.GetValue(obj, null));
                }
            } else
                sb.Append("\r\n"+ getLevelPrefix(level) + "None");

            return sb.ToString();
        }

        private static string getLevelPrefix(int level) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < level; i++) {
                stringBuilder.Append("    -");
            }
            return stringBuilder.ToString();
        }