Skip to content

Commit

Permalink
Refactored SRML.Editor into a separate project and added new FieldRep…
Browse files Browse the repository at this point in the history
…lacer system based on ScriptableObjects

Removed TestDependency
  • Loading branch information
veesusmikelheir committed Mar 22, 2019
1 parent 469bb27 commit 52d57b6
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 127 deletions.
54 changes: 54 additions & 0 deletions SMRL.Editor/FieldReplacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;

namespace SRML.Editor
{
[CreateAssetMenu(menuName = "SRML/Replacers/FieldReplacer")]
public class FieldReplacer : ScriptableObject
{
public InstanceInfo InstanceInfo;

public bool ReplaceInChildren;

[SerializeField]
public List<FieldReplacement> FieldReplacements;// = new List<Replacement>();

}
[CreateAssetMenu(menuName = "SRML/Replacers/FieldReplacement")]
public class FieldReplacement : ScriptableObject //if this isnt a scriptable object unity absolutely refuses to serialize it correctly
{

public string fieldToReplaceType;
public string fieldToReplaceFieldName;

public string replacementSourceType;
public string replacementSourceFieldName;

public bool TryResolveSource(out FieldInfo field)
{
return Resolve(replacementSourceType, replacementSourceFieldName, out field);
}
public bool TryResolveTarget(out FieldInfo field)
{
return Resolve(fieldToReplaceType,fieldToReplaceFieldName,out field);
}

private bool Resolve(String typeName, String fieldName, out FieldInfo field)
{
Debug.Log("Found "+ Type.GetType(typeName + ", Assembly-CSharp"));
if (Type.GetType(typeName+", Assembly-CSharp") is System.Type type &&
type.GetField(fieldName) is FieldInfo foundField)
{
field = foundField;
return true;
}

field = null;
return false;
}
}
}
13 changes: 13 additions & 0 deletions SMRL.Editor/FieldReplacerContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SRML.Editor
{
public class FieldReplacerContainer : MonoBehaviour
{
public FieldReplacer Replacer;
}
}
24 changes: 24 additions & 0 deletions SMRL.Editor/InstanceInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SRML.Editor
{
[CreateAssetMenu(menuName = "SRML/Replacers/InstanceInfo")]
public class InstanceInfo : ScriptableObject
{
public IDType idType;
public int ID;

public enum IDType
{
IDENTIFIABLE,
GADGET,
LANDPLOT
// to be continued
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestDependency")]
[assembly: AssemblyTitle("SMRL.Editor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestDependency")]
[assembly: AssemblyProduct("SMRL.Editor")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand All @@ -20,7 +20,7 @@
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("01e41e1d-2a2a-4959-95e1-59faa217a244")]
[assembly: Guid("d462e179-1c78-452b-94bf-64abaaf2ab35")]

// Version information for an assembly consists of the following four values:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{01E41E1D-2A2A-4959-95E1-59FAA217A244}</ProjectGuid>
<ProjectGuid>{D462E179-1C78-452B-94BF-64ABAAF2AB35}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestDependency</RootNamespace>
<AssemblyName>TestDependency</AssemblyName>
<RootNamespace>SRML.Editor</RootNamespace>
<AssemblyName>SRML.Editor</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
Expand Down Expand Up @@ -37,22 +37,16 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SRML\SRML.csproj">
<Project>{5733599a-5318-4a2c-9998-f7e0cdc664de}</Project>
<Name>SRML</Name>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\..\..\..\..\..\Program Files\Epic Games\SlimeRancher\SlimeRancher_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</ProjectReference>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="modinfo.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Compile Include="FieldReplacer.cs" />
<Compile Include="FieldReplacerContainer.cs" />
<Compile Include="InstanceInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
10 changes: 5 additions & 5 deletions SRML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SRMLInstaller", "SRMLInstal
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleMod", "SampleMod\SampleMod.csproj", "{9593A86F-00E8-4F6B-BCB1-04D049250085}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestDependency", "TestDependency\TestDependency.csproj", "{01E41E1D-2A2A-4959-95E1-59FAA217A244}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SRML.Editor", "SMRL.Editor\SRML.Editor.csproj", "{D462E179-1C78-452B-94BF-64ABAAF2AB35}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -29,10 +29,10 @@ Global
{9593A86F-00E8-4F6B-BCB1-04D049250085}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9593A86F-00E8-4F6B-BCB1-04D049250085}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9593A86F-00E8-4F6B-BCB1-04D049250085}.Release|Any CPU.Build.0 = Release|Any CPU
{01E41E1D-2A2A-4959-95E1-59FAA217A244}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01E41E1D-2A2A-4959-95E1-59FAA217A244}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01E41E1D-2A2A-4959-95E1-59FAA217A244}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01E41E1D-2A2A-4959-95E1-59FAA217A244}.Release|Any CPU.Build.0 = Release|Any CPU
{D462E179-1C78-452B-94BF-64ABAAF2AB35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D462E179-1C78-452B-94BF-64ABAAF2AB35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D462E179-1C78-452B-94BF-64ABAAF2AB35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D462E179-1C78-452B-94BF-64ABAAF2AB35}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 0 additions & 16 deletions SRML/Editor/GameObjectReplacer.cs

This file was deleted.

27 changes: 27 additions & 0 deletions SRML/Editor/ReplacerCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SRML.Editor;

namespace SRML.Editor
{
internal static class ReplacerCache
{
static Dictionary<FieldReplacer, ResolvedReplacer> replacers = new Dictionary<FieldReplacer, ResolvedReplacer>();

public static ResolvedReplacer GetReplacer(FieldReplacer replacer)
{
if (replacers.TryGetValue(replacer, out var resolved)) return resolved;
var newreplacer = ResolvedReplacer.Resolve(replacer);
replacers.Add(replacer,newreplacer);
return newreplacer;
}

public static void ClearCache()
{
replacers.Clear();
}

}
}
28 changes: 28 additions & 0 deletions SRML/Editor/ResolvedInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SRML.Editor;

namespace SRML.Editor
{
internal class ResolvedInstance
{
public object Instance { get; private set; }
public static ResolvedInstance Resolve(InstanceInfo info)
{
var instance = new ResolvedInstance();
switch (info.idType)
{
case InstanceInfo.IDType.IDENTIFIABLE:
instance.Instance = GameContext.Instance.LookupDirector.GetPrefab((Identifiable.Id) info.ID);
break;
default:
throw new NotImplementedException();
}

return instance;
}

}
}
30 changes: 30 additions & 0 deletions SRML/Editor/ResolvedReplacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using SRML.Editor;

namespace SRML.Editor
{
internal class ResolvedReplacer
{
public ResolvedInstance InstanceInfo;
public Dictionary<FieldInfo,FieldInfo> FieldToField = new Dictionary<FieldInfo, FieldInfo>();

public static ResolvedReplacer Resolve(FieldReplacer replacer)
{
ResolvedReplacer rep = new ResolvedReplacer();
rep.InstanceInfo = ResolvedInstance.Resolve(replacer.InstanceInfo);
if (replacer.FieldReplacements == null) throw new Exception("No replacements found!");
foreach (var v in replacer.FieldReplacements)
{
if (!v.TryResolveTarget(out var field1) || !v.TryResolveSource(out var field2))
throw new Exception($"Unable to resolve field! {v.replacementSourceFieldName}:{v.replacementSourceType} from {v.fieldToReplaceFieldName}:{v.fieldToReplaceType}");
rep.FieldToField.Add(field1,field2);
}

return rep;
}
}
}
3 changes: 3 additions & 0 deletions SRML/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Text;
using Harmony;
using SRML.Editor;
using SRML.Utils;
using TMPro;
using UnityEngine;
Expand Down Expand Up @@ -42,6 +43,7 @@ public static void PreLoad()
{
ErrorGUI.CreateError($"{e.Message}\nAborting mod loading...");
}
ReplacerCache.ClearCache();

}

Expand All @@ -51,6 +53,7 @@ static void PostLoad()
{
if (isPostInitialized) return;
isPostInitialized = true;
PrefabUtils.ProcessReplacements();
SRModLoader.PostLoadMods();
}

Expand Down
23 changes: 0 additions & 23 deletions SRML/SR/Editor/Replacers/LookupGameObjectReplacer.cs

This file was deleted.

21 changes: 0 additions & 21 deletions SRML/SR/Editor/Replacers/LookupGameObjectReplacerWithName.cs

This file was deleted.

13 changes: 9 additions & 4 deletions SRML/SRML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DependencyChecker.cs" />
<Compile Include="Editor\GameObjectReplacer.cs" />
<Compile Include="Editor\ReplacerCache.cs" />
<Compile Include="Editor\ResolvedInstance.cs" />
<Compile Include="Editor\ResolvedReplacer.cs" />
<Compile Include="EnumPatcher.cs" />
<Compile Include="ErrorGUI.cs" />
<Compile Include="FileSystem.cs" />
Expand All @@ -85,8 +87,6 @@
<Compile Include="Utils\ReflectionUtils.cs" />
<Compile Include="SRMod.cs" />
<Compile Include="SRModLoader.cs" />
<Compile Include="SR\Editor\Replacers\LookupGameObjectReplacer.cs" />
<Compile Include="SR\Editor\Replacers\LookupGameObjectReplacerWithName.cs" />
<Compile Include="SR\IExtendedModel.cs" />
<Compile Include="Utils\GameObjectUtils.cs" />
<Compile Include="Utils\BinaryUtils.cs" />
Expand All @@ -96,7 +96,12 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\SMRL.Editor\SRML.Editor.csproj">
<Project>{d462e179-1c78-452b-94bf-64abaaf2ab35}</Project>
<Name>SRML.Editor</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy /Y "$(TargetPath)" "$(GameDir)\"</PostBuildEvent>
Expand Down
Loading

0 comments on commit 52d57b6

Please sign in to comment.