Skip to content

Commit

Permalink
Merge pull request #28 from FedeArre/dev
Browse files Browse the repository at this point in the history
Merge dev into master
  • Loading branch information
FedeArre authored Oct 25, 2022
2 parents 3de5e1d + 6e3b8b7 commit 4ca0188
Show file tree
Hide file tree
Showing 31 changed files with 2,143 additions and 253 deletions.
152 changes: 152 additions & 0 deletions SimplePartLoader/FurnitureManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Assets.SimpleLocalization;
using Newtonsoft.Json;
using SimplePartLoader.Objects.Furniture;
using SimplePartLoader.Objects.Furniture.Saving;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace SimplePartLoader
{
internal class FurnitureManager
{
private static Hashtable FurnitureList = new Hashtable();
internal static List<SaleFurniture> SaleItems = new List<SaleFurniture>();

private static FurnitureWrapper SaveData = new FurnitureWrapper();

internal static string SavePath = $"{Application.persistentDataPath}/save1/modSaves/";
internal static string FileName = "ModUtilsFurniture.json";

public static Hashtable Furnitures
{
get { return FurnitureList; }
}

public static void LoadFurniture()
{
ModUtils.GetPlayer().AddComponent<PlayerFurniturePickup>();

GameObject spawnSpot = new GameObject("SPAWN_POINT_FURNITURE_MODUTILS");
spawnSpot.transform.position = new Vector3(660.2147f, 56.3729f, -83.6104f);

foreach(SaleFurniture sf in SaleItems)
{
GameObject furniture = GameObject.Instantiate(sf.Furniture.Prefab);

furniture.layer = LayerMask.NameToLayer("Items");

GameObject.Destroy(furniture.GetComponent<MooveItem>());
GameObject.Destroy(furniture.GetComponent<ModUtilsFurniture>());
GameObject.DestroyImmediate(furniture.GetComponent<Rigidbody>());

SaleItem si = furniture.AddComponent<SaleItem>();
si.Price = sf.Furniture.Price;
si.Item = sf.Furniture.Prefab;
si.SpawnSpot = spawnSpot;

furniture.transform.position = sf.Pos;
furniture.transform.eulerAngles = sf.Rot;

furniture.name = sf.Furniture.Name;

// Localization of the part
foreach (var dictionary in LocalizationManager.Dictionary)
{
if (dictionary.Value.ContainsKey(sf.Furniture.Prefab.name)) // Ignore case where the name is shared so the translation already exists
continue;

dictionary.Value.Add(sf.Furniture.Prefab.name, sf.Furniture.Name);
}
}

// Check if new save.
if (PlayerPrefs.GetFloat("LoadLevel") == 0f) // New game check
{
if (File.Exists(SavePath + FileName))
File.Delete(SavePath + FileName);

return;
}

if (!File.Exists(SavePath + FileName))
return;

Debug.Log($"[ModUtils/Furniture/Loader]: Starting furniture loader.");
// We load the data now
using (StreamReader r = new StreamReader(SavePath + FileName))
{
string json = r.ReadToEnd();

if (String.IsNullOrEmpty(json))
return;

SaveData = JsonConvert.DeserializeObject<FurnitureWrapper>(json);
}

Debug.Log($"[ModUtils/Furniture/Loader]: Trying to load {SaveData.Furnitures.Count} furnitures.");
// And then with the data loaded we load the Furnitures
foreach (FurnitureData fd in SaveData.Furnitures)
{
Furniture f = (Furniture) Furnitures[fd.PrefabName];
if(f != null)
{
GameObject furniture = GameObject.Instantiate(f.Prefab);
furniture.name = f.Prefab.name;

furniture.transform.position = new Vector3(fd.X, fd.Y, fd.Z);
furniture.transform.eulerAngles = new Vector3(fd.rX, fd.rY, fd.rZ);

if (furniture.GetComponent<Rigidbody>() && f.BehaveAsFurniture)
GameObject.DestroyImmediate(furniture.GetComponent<Rigidbody>());
}
else
{
Debug.Log("[ModUtils/Furniture/Loader]: A furniture was loaded but not found in-game! Name: " + fd.PrefabName);
}
}
Debug.Log($"[ModUtils/Furniture/Loader]: Furniture load finished.");
}

public static void SaveFurniture()
{
SaveData = new FurnitureWrapper();

ModUtilsFurniture[] AllFurnitures = UnityEngine.Object.FindObjectsOfType<ModUtilsFurniture>();
Debug.Log($"[ModUtils/Furniture/Saver]: Trying to save {AllFurnitures.Length} furnitures.");

foreach (ModUtilsFurniture f in AllFurnitures)
{
FurnitureData fw = new FurnitureData();

fw.PrefabName = f.furnitureRef.PrefabName;

Vector3 fix = ModUtils.UnshiftCoords(f.transform.position);
fw.X = fix.x;
fw.Y = fix.y;
fw.Z = fix.z;

fw.rX = f.transform.eulerAngles.x;
fw.rY = f.transform.eulerAngles.y;
fw.rZ = f.transform.eulerAngles.z;

SaveData.Furnitures.Add(fw);
}

if (!Directory.Exists(SavePath))
Directory.CreateDirectory(SavePath);

using (TextWriter tw = new StreamWriter(SavePath + FileName))
{
tw.Write(JsonConvert.SerializeObject(SaveData));
}

Debug.Log($"[ModUtils/Furniture/Saver]: Succesfully saved {AllFurnitures.Length} furnitures.");
}
}
}
35 changes: 23 additions & 12 deletions SimplePartLoader/ModMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class ModMain : Mod
public override string ID => "ModUtils";
public override string Name => "ModUtils";
public override string Author => "Federico Arredondo";
public override string Version => "v1.0.0"; bool TESTING_VERSION_REMEMBER = true;

public override string Version => "v1.2.0";

bool TESTING_VERSION_REMEMBER = false;
string TESTING_VERSION_NUMBER = "1.2-rc3";

public override byte[] Icon => Properties.Resources.SimplePartLoaderIcon;


// Autoupdater
public const string API_URL = "https://mygaragemod.xyz/api";
GameObject UI_Prefab, UI_Error_Prefab, UI, UI_BrokenInstallation_Prefab, UI_DeveloperLogEnabled_Prefab;
Expand All @@ -46,7 +48,7 @@ public ModMain()
Debug.Log("ModUtils is loading - Version: " + Version);
Debug.Log("Developed by Federico Arredondo - www.github.com/FedeArre");
if(TESTING_VERSION_REMEMBER)
Debug.Log("This is a testing version - remember to report bugs and send feedback");
Debug.Log($"This is a testing version ({TESTING_VERSION_NUMBER}) - remember to report bugs and send feedback");

// Mod delete
string ModsFolderPath = Application.dataPath + "/../Mods/";
Expand Down Expand Up @@ -80,13 +82,20 @@ public ModMain()
UI_DeveloperLogEnabled_Prefab = AutoupdaterBundle.LoadAsset<GameObject>("CanvasDevLog");

UI_Prefab.GetComponent<Canvas>().sortingOrder = 1; // Fixes canva disappearing after a bit.

UI_Error_Prefab.GetComponent<Canvas>().sortingOrder = 1;

PaintingSystem.BackfaceShader = AutoupdaterBundle.LoadAsset<Shader>("BackfaceShader");
PaintingSystem.CullBaseMaterial = AutoupdaterBundle.LoadAsset<Material>("testMat");
AutoupdaterBundle.Unload(false);

ModUtils.SetupSteamworks();
}

public override void OnMenuLoad()
{
string autoupdaterDirectory = Path.Combine(Application.dataPath, "..\\Mods\\NewAutoupdater");
string autoupdaterPath = autoupdaterDirectory + "\\Autoupdater.exe";

if (!MenuFirstLoad)
{
MenuFirstLoad = true;
Expand All @@ -95,13 +104,17 @@ public override void OnMenuLoad()
{
Debug.Log($"{m.Name} (ID: {m.ID}) - Version {m.Version}");
}

// Enable heartbeat

if (!File.Exists(autoupdaterPath + "\\disableStatus.txt"))
KeepAlive.GetInstance().Ready();

return;
}
Debug.Log("[ModUtils/Autoupdater]: Autoupdater check");

// Check for broken ModUtils autoupdater installation
string autoupdaterDirectory = Path.Combine(Application.dataPath, "..\\Mods\\NewAutoupdater");
string autoupdaterPath = autoupdaterDirectory + "\\Autoupdater.exe";
bool brokenInstallation = false;

if(!Directory.Exists(autoupdaterDirectory) || !File.Exists(autoupdaterPath))
Expand Down Expand Up @@ -170,17 +183,14 @@ public override void OnMenuLoad()
Debug.Log("[ModUtils/Autoupdater/Error]: Error occured while trying to fetch updates, error: " + ex.ToString());
GameObject.Instantiate(UI_Error_Prefab);
}

// Enable heartbeat
KeepAlive.GetInstance().Ready();

}

public override void OnLoad()
{
ModUtils.OnLoadCalled();
PartManager.OnLoadCalled();

FurnitureManager.LoadFurniture();

PlayerTransform = ModUtils.GetPlayer().transform;

if(PlayerPrefs.GetFloat("LoadLevel") == 0f)
Expand Down Expand Up @@ -242,6 +252,7 @@ public override void OnSaveFinish()
return;

CustomSaverHandler.Save();
FurnitureManager.SaveFurniture();
}

// For mod utils
Expand Down
36 changes: 36 additions & 0 deletions SimplePartLoader/ModUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mirror">
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Mirror.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mirror.Authenticators">
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Mirror.Authenticators.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mirror.Components">
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Mirror.Components.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Newtonsoft.Json.dll</HintPath>
Expand All @@ -57,9 +69,15 @@
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\Rewired_Windows.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="RuntimePreviewGenerator.Runtime, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>F:\SteamLibrary\steamapps\common\My Garage\My Garage_Data\Managed\RuntimePreviewGenerator.Runtime.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -436,7 +454,24 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Autoupdating.cs" />
<Compile Include="FurnitureManager.cs" />
<Compile Include="ModUtils\ModObjects\EACheck.cs" />
<Compile Include="ModUtils\ModObjects\EarlyAccessJson.cs" />
<Compile Include="ModUtils\ModObjects\ModInstance.cs" />
<Compile Include="ModUtils\ModObjects\ModSettings.cs" />
<Compile Include="Objects\EditorComponents\ChildDestroy.cs" />
<Compile Include="Objects\EditorComponents\ChildMove.cs" />
<Compile Include="Objects\EditorComponents\FurnitureGenerator.cs" />
<Compile Include="Objects\EditorComponents\InternalMarker.cs" />
<Compile Include="Objects\EditorComponents\OriginalMesh.cs" />
<Compile Include="Objects\Furniture\Furniture.cs" />
<Compile Include="Objects\Furniture\PlayerFurniturePickup.cs" />
<Compile Include="Objects\Furniture\Saving\FurnitureData.cs" />
<Compile Include="Objects\Furniture\Saving\FurnitureWrapper.cs" />
<Compile Include="Objects\Furniture\Saving\ModUtilsFurniture.cs" />
<Compile Include="Objects\LightFix.cs" />
<Compile Include="Objects\Furniture\SaleFurniture.cs" />
<Compile Include="Utils\CommonFixes.cs" />
<Compile Include="Utils\KeepAlive.cs" />
<Compile Include="CustomSaverHandler.cs" />
<Compile Include="ModUtils\ModUtils.cs" />
Expand Down Expand Up @@ -470,6 +505,7 @@
<Compile Include="Utils\Extension.cs" />
<Compile Include="Utils\Functions.cs" />
<Compile Include="ModUtils\PaintingSystem.cs" />
<Compile Include="Utils\ModUtils_Snapshoter.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
Expand Down
38 changes: 38 additions & 0 deletions SimplePartLoader/ModUtils/ModObjects/EACheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace SimplePartLoader
{
[DisallowMultipleComponent]
internal class EACheck : MonoBehaviour
{
internal int frameCount = 0;
internal bool checkDone = false;
void Update()
{
if (checkDone)
return;

frameCount++;
if (!SteamManager.Initialized)
return;

ulong steamID = Steamworks.SteamUser.GetSteamID().m_SteamID;
Debug.Log("[ModUtils/EACheck]: Identified user: " + steamID);

foreach (ModInstance mi in ModUtils.ModInstances)
{
if (mi.RequiresSteamCheck)
{
mi.Check(steamID);
}
}

checkDone = true;
}
}
}
14 changes: 14 additions & 0 deletions SimplePartLoader/ModUtils/ModObjects/EarlyAccessJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimplePartLoader
{
internal class EarlyAccessJson
{
public string ModId;
public string SteamId;
}
}
Loading

0 comments on commit 4ca0188

Please sign in to comment.