Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit cef472d

Browse files
committed
Initial Commit
0 parents  commit cef472d

File tree

11 files changed

+878
-0
lines changed

11 files changed

+878
-0
lines changed

.gitignore

+403
Large diffs are not rendered by default.

Dependecies/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

ModLoader.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29709.97
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModLoader", "ModLoader\ModLoader.csproj", "{3BB62E16-1126-4ED9-AF39-64B88C642B14}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3BB62E16-1126-4ED9-AF39-64B88C642B14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3BB62E16-1126-4ED9-AF39-64B88C642B14}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3BB62E16-1126-4ED9-AF39-64B88C642B14}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3BB62E16-1126-4ED9-AF39-64B88C642B14}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2C567E92-8BA9-4DC1-82A3-92CF35730008}
24+
EndGlobalSection
25+
EndGlobal

ModLoader/Loader.cs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using SFS;
2+
using HarmonyLib;
3+
using UnityEngine;
4+
using SFS.IO;
5+
using System.Collections.Generic;
6+
using System;
7+
using System.Linq.Expressions;
8+
using SFS.Builds;
9+
using System.Reflection;
10+
using UnityEngine.SceneManagement;
11+
using UnityEngine.Events;
12+
13+
namespace ModLoader
14+
{
15+
/// <summary>
16+
/// This is the main class of ModLoader. this class is injected into the game whit Unity Doorstop injector.
17+
/// </summary>
18+
public class Loader : MonoBehaviour
19+
{
20+
21+
22+
// This is de main logger, use this to show message in console
23+
public static ModConsole logger;
24+
25+
private static Harmony patcher;
26+
27+
// Thiss save Loader instance
28+
public static Loader modLoader;
29+
30+
// This save the gameObject that implement Loader class
31+
public static GameObject root;
32+
public static BaseAssigner baseAssigner;
33+
34+
// List of all mods loaded in the folder MODS
35+
private SFSMod[] modList;
36+
37+
public Loader()
38+
{
39+
Loader.logger = new ModConsole();
40+
Loader.modLoader = this;
41+
}
42+
43+
public void Awake()
44+
{
45+
this.modList = new SFSMod[] { };
46+
Loader.logger.log("Loading Mods", "ModLoader");
47+
48+
// If not existe is created and pass FolderPath
49+
FolderPath modFolder = FileLocations.BaseFolder.Extend("MODS").CreateFolder();
50+
51+
// get list of files into MODS folder
52+
IEnumerable<FilePath> files = modFolder.GetFilesInFolder(false);
53+
foreach (FilePath file in files)
54+
{
55+
// get only dll files in mods folder
56+
if (file.Extension == "dll")
57+
{
58+
Loader.logger.log("Reading " + file.FileName, "ModLoader");
59+
// try to load mod
60+
this.loadMod(modFolder.GetRelativePath(file.FileName) + "/" + file.FileName);
61+
}
62+
}
63+
}
64+
65+
public SFSMod[] getModList()
66+
{
67+
return this.modList;
68+
}
69+
70+
void OnEnable()
71+
{
72+
SceneManager.sceneLoaded += OnSceneLoaded;
73+
}
74+
75+
public bool suscribeOnChangeScene(UnityAction<Scene, LoadSceneMode> method)
76+
{
77+
SceneManager.sceneLoaded += method;
78+
return true;
79+
}
80+
81+
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
82+
{
83+
logger.log("scene change to "+ scene.name, "ModLoader");
84+
}
85+
86+
private void loadMod(string path)
87+
{
88+
Assembly assembly = Assembly.LoadFrom(path);
89+
SFSMod mod = null;
90+
logger.log("Searching SFSMod interface", "ModLoader");
91+
// we search SFSMod interface in dll file dounede in MODS folder
92+
foreach (Type typeClass in assembly.GetTypes())
93+
{
94+
Type inteface = typeClass.GetInterface(typeof(SFSMod).Name);
95+
if(inteface == null)
96+
{
97+
continue;
98+
}
99+
mod = (Activator.CreateInstance(typeClass) as SFSMod);
100+
}
101+
102+
if (mod == null)
103+
{
104+
logger.log("File " + path + " don't have SFSMod interface","ModLoader");
105+
return;
106+
}
107+
108+
Loader.logger.log("SFSMod interface found", "ModLoader");
109+
110+
try
111+
{
112+
logger.log("Loading " + mod.getModName(), mod.getModAuthor());
113+
114+
// execute entry point of mod
115+
mod.load();
116+
logger.log("Loaded " + mod.getModName(), mod.getModAuthor());
117+
this.modList.AddItem(mod);
118+
}
119+
catch( Exception e)
120+
{
121+
logger.log("Error loading " + mod.getModName(), mod.getModAuthor());
122+
logger.logError(e);
123+
}
124+
}
125+
126+
/// <summary>
127+
/// This is the mod loader entry point, this is the method execute after be injected in the game
128+
/// </summary>
129+
/// <param name="args"></param>
130+
public static void Main(string[] args)
131+
{
132+
Loader.patcher = new Harmony("SFS.mod.loader");
133+
Loader.patcher.PatchAll();
134+
}
135+
}
136+
137+
}

ModLoader/ModConsole.cs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Runtime.InteropServices;
5+
using UnityEngine;
6+
using SFS.IO;
7+
8+
namespace ModLoader
9+
{
10+
11+
public class ModConsole
12+
{
13+
[DllImport("kernel32.dll")]
14+
private static extern IntPtr GetConsoleWindow();
15+
16+
[DllImport("user32.dll")]
17+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
18+
19+
[DllImport("Kernel32.dll")]
20+
private static extern bool AllocConsole();
21+
22+
private static FilePath logFile;
23+
24+
public ModConsole()
25+
{
26+
ModConsole.AllocConsole();
27+
Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
28+
{
29+
AutoFlush = true
30+
});
31+
this.visible = true;
32+
DateTime current = new DateTime();
33+
ModConsole.logFile = FileLocations.BaseFolder.Extend("logs").CreateFolder().ExtendToFile(current.Year + "-" + current.Month + "-" + current.Day+".txt");
34+
}
35+
36+
private void hideConsole()
37+
{
38+
ModConsole.ShowWindow(ModConsole.GetConsoleWindow(), 0);
39+
this.visible = false;
40+
}
41+
42+
private void showConsole()
43+
{
44+
ModConsole.ShowWindow(ModConsole.GetConsoleWindow(), 5);
45+
this.visible = true;
46+
}
47+
48+
public void toggleConsole()
49+
{
50+
bool flag = this.visible;
51+
if (flag)
52+
{
53+
this.hideConsole();
54+
}
55+
else
56+
{
57+
this.showConsole();
58+
}
59+
}
60+
61+
public void logError(Exception e)
62+
{
63+
StackTrace stackTrace = new StackTrace(e, true);
64+
StackFrame frame = stackTrace.GetFrame(0);
65+
int fileColumnNumber = frame.GetFileColumnNumber();
66+
int fileLineNumber = frame.GetFileLineNumber();
67+
string fileName = frame.GetFileName();
68+
this.tryLogCustom("##[ERROR]##", "ErrorReporter", LogType.Error);
69+
this.tryLogCustom(e.Message , "ErrorReporter", LogType.Error);
70+
this.tryLogCustom(e.StackTrace, "ErrorReporter", LogType.Error);
71+
this.tryLogCustom(fileLineNumber+":"+ fileColumnNumber + "@" + fileName, "ErrorReporter", LogType.Error);
72+
this.tryLogCustom("##[ERROR]##", "ErrorReporter", LogType.Error);
73+
}
74+
75+
public void log(string msg, string tag)
76+
{
77+
string logMessage = "[" + tag + "]: " + msg;
78+
Console.WriteLine(logMessage);
79+
logFile.AppendText(logMessage+"\n");
80+
}
81+
82+
public void log(string msg)
83+
{
84+
this.log(msg, "Unkwn");
85+
}
86+
87+
private void tryLogCustom(string msg, string tag, LogType type)
88+
{
89+
bool flag = this.logCustom == null;
90+
if (flag)
91+
{
92+
this.log(msg, tag);
93+
}
94+
else
95+
{
96+
msg = "[" + tag + "]: " + msg;
97+
try
98+
{
99+
this.logCustom(msg, type);
100+
}
101+
catch (Exception e)
102+
{
103+
this.logError(e);
104+
}
105+
}
106+
}
107+
108+
private void tryLogCustom(string msg, LogType type)
109+
{
110+
this.tryLogCustom(msg, "Unkwn", type);
111+
}
112+
113+
public void setLogger(Action<string, LogType> logfunc)
114+
{
115+
this.logCustom = logfunc;
116+
}
117+
118+
private const int SW_HIDE = 0;
119+
120+
private const int SW_SHOW = 5;
121+
122+
private bool visible = false;
123+
124+
125+
private Action<string, LogType> logCustom;
126+
}
127+
}

ModLoader/ModLoader.csproj

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3BB62E16-1126-4ED9-AF39-64B88C642B14}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ModLoader</RootNamespace>
11+
<AssemblyName>ModLoader</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="0Harmony, Version=2.2.0.0, Culture=neutral, processorArchitecture=MSIL">
35+
<HintPath>..\packages\Lib.Harmony.2.2.0\lib\net45\0Harmony.dll</HintPath>
36+
</Reference>
37+
<Reference Include="Assembly-CSharp">
38+
<HintPath>..\Dependecies\Assembly-CSharp.dll</HintPath>
39+
<Private>False</Private>
40+
</Reference>
41+
<Reference Include="System" />
42+
<Reference Include="System.Core" />
43+
<Reference Include="System.Xml.Linq" />
44+
<Reference Include="System.Data.DataSetExtensions" />
45+
<Reference Include="Microsoft.CSharp" />
46+
<Reference Include="System.Data" />
47+
<Reference Include="System.Net.Http" />
48+
<Reference Include="System.Xml" />
49+
<Reference Include="UnityEngine">
50+
<HintPath>..\Dependecies\UnityEngine.dll</HintPath>
51+
<Private>False</Private>
52+
</Reference>
53+
<Reference Include="UnityEngine.CoreModule">
54+
<HintPath>..\Dependecies\UnityEngine.CoreModule.dll</HintPath>
55+
<Private>False</Private>
56+
</Reference>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="Loader.cs" />
60+
<Compile Include="ModConsole.cs" />
61+
<Compile Include="ModsMenu.cs" />
62+
<Compile Include="Patcher.cs" />
63+
<Compile Include="Properties\AssemblyInfo.cs" />
64+
<Compile Include="SFSMod.cs" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
70+
</Project>

ModLoader/ModsMenu.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
3+
using UnityEngine;
4+
5+
namespace ModLoader
6+
{
7+
class ModsMenu : MonoBehaviour
8+
{
9+
10+
}
11+
}

0 commit comments

Comments
 (0)