Skip to content

Commit

Permalink
change net 6 start up to be consistent timing wise with net 4 start up
Browse files Browse the repository at this point in the history
add support for yml mapping Vector3/2 and other types
  • Loading branch information
Tyler-IN committed Mar 28, 2022
1 parent ecc085e commit 746ccd8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 26 deletions.
12 changes: 12 additions & 0 deletions .run/Clean Deployed.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Clean Deployed" type="RunNativeExe" factoryName="Native Executable">
<option name="EXE_PATH" value="$PROJECT_DIR$/../../../../../Windows/System32/cmd.exe" />
<option name="PROGRAM_PARAMETERS" value="/c del /f &quot;DistantWorlds2.ModLoader.*&quot; &amp;&amp; del /f &quot;DW2Net6Win.*&quot;" />
<option name="WORKING_DIRECTORY" value="D:/Steam/steamapps/common/Distant Worlds 2/" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<method v="2">
<option name="Build Solution" enabled="true" />
</method>
</configuration>
</component>
2 changes: 1 addition & 1 deletion .run/Overwrite DW2Net6Win (Debug).run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Overwrite DW2Net6Win (Debug)" type="RunNativeExe" factoryName="Native Executable" folderName="DW2Net6Win">
<option name="EXE_PATH" value="$PROJECT_DIR$/../../../../../Windows/System32/cmd.exe" />
<option name="PROGRAM_PARAMETERS" value="/c copy /y &quot;DW2Net6Win\bin\Debug\net6.0-windows\publish\*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="PROGRAM_PARAMETERS" value="/c del /f &quot;D:\Steam\steamapps\common\Distant Worlds 2\DW2Net6Win.*&quot; &amp;&amp; copy /y &quot;DW2Net6Win\bin\Debug\net6.0-windows\publish\*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion .run/Overwrite DW2Net6Win (Release).run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Overwrite DW2Net6Win (Release)" type="RunNativeExe" factoryName="Native Executable" folderName="DW2Net6Win">
<option name="EXE_PATH" value="$PROJECT_DIR$/../../../../../Windows/System32/cmd.exe" />
<option name="PROGRAM_PARAMETERS" value="/c copy /y &quot;DW2Net6Win\bin\Release\net6.0-windows\publish\*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="PROGRAM_PARAMETERS" value="/c del /f &quot;D:\Steam\steamapps\common\Distant Worlds 2\DW2Net6Win.*&quot; &amp;&amp; copy /y &quot;DW2Net6Win\bin\Release\net6.0-windows\publish\*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
Expand Down
4 changes: 2 additions & 2 deletions .run/Overwrite DistantWorlds2.ModLoader (Debug).run.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Overwrite DistantWorlds2.ModLoader (Debug)" type="RunNativeExe" factoryName="Native Executable" folderName="DistantWorlds2.ModLoader">
<option name="EXE_PATH" value="$PROJECT_DIR$/../../../../../Windows/System32/cmd.exe" />
<option name="PROGRAM_PARAMETERS" value="/c copy /v /y &quot;DistantWorlds2.ModLoader\bin\Debug\net472\DistantWorlds2.ModLoader.*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="PROGRAM_PARAMETERS" value="/c del /f &quot;D:\Steam\steamapps\common\Distant Worlds 2\DistantWorlds2.ModLoader.*&quot; &amp;&amp; copy /v /y &quot;DistantWorlds2.ModLoader\bin\Debug\net472\DistantWorlds2.ModLoader.*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<method v="2">
<option name="Build Solution" enabled="true" />
<option name="Build" default="false" projectName="DistantWorlds2.ModLoader" projectPath="file://$PROJECT_DIR$/DistantWorlds2.ModLoader/DistantWorlds2.ModLoader.csproj" />
</method>
</configuration>
</component>
4 changes: 2 additions & 2 deletions .run/Overwrite DistantWorlds2.ModLoader (Release).run.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Overwrite DistantWorlds2.ModLoader (Release)" type="RunNativeExe" factoryName="Native Executable" folderName="DistantWorlds2.ModLoader">
<option name="EXE_PATH" value="$PROJECT_DIR$/../../../../../Windows/System32/cmd.exe" />
<option name="PROGRAM_PARAMETERS" value="/c copy /v /y &quot;DistantWorlds2.ModLoader\bin\Release\net472\DistantWorlds2.ModLoader.*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="PROGRAM_PARAMETERS" value="/c del /f &quot;D:\Steam\steamapps\common\Distant Worlds 2\DistantWorlds2.ModLoader.*&quot; &amp;&amp; copy /v /y &quot;DistantWorlds2.ModLoader\bin\Release\net472\DistantWorlds2.ModLoader.*&quot; &quot;D:\Steam\steamapps\common\Distant Worlds 2&quot;" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<method v="2">
<option name="Build Solution" enabled="true" />
<option name="Build" default="false" projectName="DistantWorlds2.ModLoader" projectPath="file://$PROJECT_DIR$/DistantWorlds2.ModLoader/DistantWorlds2.ModLoader.csproj" />
</method>
</configuration>
</component>
23 changes: 11 additions & 12 deletions DW2Net6Win/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static class Program
public static Assembly EntryAssembly = null!;
private static object? _dwGame;
private static readonly Harmony Harmony = new Harmony("DW2Net6Win");
private static object? _modLoader;

public static int Main(string[] args)
{
Expand All @@ -46,7 +45,18 @@ public static int Main(string[] args)
};

EntryAssembly = Assembly.LoadFile(Path.Combine(Environment.CurrentDirectory, "DistantWorlds2.exe"));

var mlPath = Path.Combine(Environment.CurrentDirectory, "DistantWorlds2.ModLoader.dll");

if (File.Exists(mlPath))
{
var mlAsm = Assembly.LoadFile(mlPath)!;
var startUpType = mlAsm.GetType("DistantWorlds2.ModLoader.StartUp");
startUpType?.InvokeMember("InitializeModLoader",
BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod,
null, null, null);
}

bool ohNo;

try
Expand All @@ -66,17 +76,6 @@ public static int Main(string[] args)

ss.Show(true);

var mlPath = Path.Combine(Environment.CurrentDirectory, "DistantWorlds2.ModLoader.dll");

if (File.Exists(mlPath))
{
var mlAsm = Assembly.LoadFile(mlPath)!;
var startUpType = mlAsm.GetType("DistantWorlds2.ModLoader.StartUp");
startUpType?.InvokeMember("InitializeModLoader",
BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod,
null, null, null);
}

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var dwAsm = assemblies.FirstOrDefault(a => a.GetName().Name == "DistantWorlds2");
if (dwAsm is null)
Expand Down
51 changes: 44 additions & 7 deletions DistantWorlds2.ModLoader.Core/GameDataDefinitionPatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,7 @@ private static object Prepopulate(object obj, Type? objType = null)
if (obj is null) throw new ArgumentNullException(nameof(obj));

objType ??= obj.GetType();
if (!objType.IsClass) return obj;
if (objType == typeof(string)) return "";
if (!objType.IsClass || objType == typeof(string)) return obj;
var fieldOrProps = objType
.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(m => m is FieldInfo { IsStatic: false } or PropertyInfo { GetMethod.IsStatic: false });
Expand All @@ -892,9 +891,7 @@ private static object Prepopulate(object obj, Type? objType = null)
var fieldOrPropType = GetFieldOrPropertyType(fieldOrProp);
if (!fieldOrPropType.IsClass) continue;
if (GetValue(obj, fieldOrProp) is null)
SetValue(obj, fieldOrProp, fieldOrPropType != typeof(string)
? Prepopulate(Activator.CreateInstance(fieldOrPropType), fieldOrPropType)
: "");
SetValue(obj, fieldOrProp, Prepopulate(Activator.CreateInstance(fieldOrPropType), fieldOrPropType));
}

return obj;
Expand Down Expand Up @@ -1341,6 +1338,8 @@ private static object ProcessObjectUpdate(Type type, object obj, YamlMappingNode
var valNode = kv.Value;

if (typeof(IList).IsAssignableFrom(valType))
{
initValue ??= CreateInstance(valType);
switch (valNode)
{
case YamlScalarNode scalar: {
Expand All @@ -1359,6 +1358,7 @@ private static object ProcessObjectUpdate(Type type, object obj, YamlMappingNode
break;
}
}
}
else if (
valType.IsPrimitive
&& Type.GetTypeCode(valType) is not
Expand Down Expand Up @@ -1447,6 +1447,32 @@ or TypeCode.Decimal
default:
throw new NotSupportedException(valNode.Start.ToString());
}
else if (valType.IsClass)
{
initValue ??= Prepopulate(CreateInstance(valType));
switch (valNode)
{
case YamlMappingNode map: {
ProcessObjectUpdate(valType, initValue, map, compileFn);
break;
}
default:
throw new NotSupportedException(valNode.Start.ToString());
}
}
else if (valType.IsValueType)
{
initValue ??= CreateInstance(valType);
switch (valNode)
{
case YamlMappingNode map: {
ProcessObjectUpdate(valType, initValue, map, compileFn);
break;
}
default:
throw new NotSupportedException(valNode.Start.ToString());
}
}
else
Console.WriteLine($"Warning, field with unsupported type {valType.FullName} @ {valNode.Start}");
}
Expand All @@ -1468,9 +1494,12 @@ or TypeCode.Decimal
collection[index] = ProcessCollectionItemUpdate(valNode, itemType, collection[index], compileFn);
else
collection.Add(ProcessCollectionItemUpdate(valNode, itemType,
itemType == typeof(string) ? "" : Activator.CreateInstance(collectionType), compileFn));
CreateInstance(itemType), compileFn));
}
}
private static object CreateInstance(Type itemType)
=> itemType == typeof(string) ? "" : Activator.CreateInstance(itemType);

private static void ParseCollectionUpdate(Type collectionType, IList collection, YamlMappingNode item,
Func<object, string, Func<object>> compileFn)
{
Expand Down Expand Up @@ -1502,8 +1531,16 @@ or TypeCode.Decimal
}
}

var initValue = collection[idVal];
object initValue;
if (collection.Count > idVal)
{
initValue = collection[idVal];

}
else
{
initValue = Prepopulate(CreateInstance(itemType));
}
var valNode = kv.Value;

ProcessCollectionItemUpdate(valNode, itemType, initValue, compileFn);
Expand Down
26 changes: 25 additions & 1 deletion DistantWorlds2.ModLoader/StartUp.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using System.Text;
using JetBrains.Annotations;
using Xenko.Core.MicroThreading;
using Xenko.Engine;

namespace DistantWorlds2.ModLoader;

[PublicAPI]
public static class StartUp
{
public static void InitializeModLoader()
private static bool _initialized;
private static bool _started;

public static void StartModLoader()
{
if (_started) return;
_started = true;

ConsoleHelper.CreateConsole();

var debug = Environment.GetEnvironmentVariable("DW2MC_DEBUG");
Expand All @@ -29,4 +39,18 @@ public static void InitializeModLoader()
ModLoader.Patches = new Patches();
ModLoader.ModManager = new ModManager();
}
public static void InitializeModLoader()
{
if (_initialized) return;
_initialized = true;

AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoadHandler;
}
private static void AssemblyLoadHandler(object sender, AssemblyLoadEventArgs args)
{
var asmName = args.LoadedAssembly.GetName();
if (asmName.Name != "DistantWorlds2") return;
AppDomain.CurrentDomain.AssemblyLoad -= AssemblyLoadHandler;
StartModLoader();
}
}

0 comments on commit 746ccd8

Please sign in to comment.