Skip to content

Commit

Permalink
add custom save data docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Nov 22, 2023
1 parent 6f2b868 commit 2aaf064
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 14 deletions.
9 changes: 0 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
- Fixed key rebinding not working when done from in game.
- Default key can now be set using enum.
- Added tooltip provider.
- Settings entries can now have headers and spacing.
- Settings entries will now show the description in a tooltip.
- New splash screen.
- Mods can have loader version constraints now.
- Mod list will show if (and why) a mod isn't loaded.
- Added support for saving custom data to save files.
40 changes: 35 additions & 5 deletions SonsSdk/ConstructionTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ public static StructureRecipe GetRecipe(int id)
{
return null;
}

return ScrewStructureManager._instance._database._recipes._items.FirstOrDefault(x => x._id == id);

// Recipes in the list can be null.
foreach (var recipe in ScrewStructureManager._instance._database._recipes)
{
if(!recipe)
continue;

if (recipe._id == id)
return recipe;
}

return null;
}

public static StructureRecipe GetRecipe(string name)
Expand All @@ -22,16 +32,36 @@ public static StructureRecipe GetRecipe(string name)
return null;
}

return ScrewStructureManager._instance._database._recipes._items.FirstOrDefault(x => x.Name == name);
}
// Recipes in the list can be null.
foreach (var recipe in ScrewStructureManager._instance._database._recipes)
{
if(!recipe)
continue;

if (recipe.Name == name)
return recipe;
}

return null;
}

public static ElementProfile GetProfile(int id)
{
if (!ElementProfileDatabase._instance)
{
return null;
}

return ElementProfileDatabase._instance._profiles._items.FirstOrDefault(x => x._id == id);
// Profiles in the list can be null.
foreach (var profile in ElementProfileDatabase._instance._profiles)
{
if(!profile)
continue;

if (profile._id == id)
return profile;
}

return null;
}
}
45 changes: 45 additions & 0 deletions docfx_project/articles/custom-save-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Adding custom data to a save file
Red Loader has utilities if you want your mod to persist data in save games.
This is useful if you want to save settings or other data that you want to persist between game sessions for a particular save.

## Serializer class
Your mod needs some kind of class to serialize and deserialize your data.
A common way is to have some kind of "manager" class that holds all the data you want to save.

```csharp
public class MyModManager : ICustomSaveable<MyModManager.MyModSaveData>
{
public string Name => "MyModManager";

// Used to determine if the data should also be saved in multiplayer saves
public bool IncludeInPlayerSave => true;

public MyModSaveData Save()
{
// Serialize your data from game state here
return new MyModSaveData();
}

public void Load(MyModSaveData obj)
{
// Apply game state from your data here
}

public class MyModSaveData
{
public Color SomeColor;
public Vector3 SomeVector;
}
}
```

The important parts here are the `ICustomSaveable` interface that `MyModManager` implements, and the `MyModSaveData` class that is used to serialize and deserialize the data.
The `Save` and `Load` methods from the `ICustomSaveable` interface are used to serialize and deserialize the data.
Red Loader will call them when a save game gets loaded or the user requests a save.

## Registering your serializer
Registering the serializer is as easy as calling the following in `OnSdkInitialized`:
```csharp
var manager = new MyModManager();
SonsSaveTools.Register(manager);
```
2 changes: 2 additions & 0 deletions docfx_project/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
href: configuration.md
- name: Asset Bundles and Addressables
href: bundles.md
- name: Custom Save Data
href: custom-save-data.md
- name: Freecam
href: freecam.md
- name: Custom Sounds
Expand Down
151 changes: 151 additions & 0 deletions docs/articles/custom-save-data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Adding custom data to a save file | RedLoader Docs </title>
<meta name="viewport" content="width=device-width">
<meta name="title" content="Adding custom data to a save file | RedLoader Docs ">
<meta name="generator" content="docfx ">

<link rel="shortcut icon" href="../https://github.com/ToniMacaroni/RedLoader/blob/16cc6bb13745526a451eab0fd1ce02f7b128916f/Resources/favicon.ico?raw=true">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/night-owl.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" integrity="sha384-EvBWSlnoFgZlXJvpzS+MAUEjvN7+gcCwH+qh7GRFOGgZO0PuwOFro7qPOJnLfe7l" crossorigin="anonymous">
<link rel="stylesheet" href="../styles/config.css">
<link rel="stylesheet" href="../styles/discord.css">
<link rel="stylesheet" href="../styles/singulink.css">
<link rel="stylesheet" href="../styles/main.css">
<meta property="docfx:navrel" content="../toc.html">
<meta property="docfx:tocrel" content="toc.html">

<meta property="docfx:rel" content="../">

</head>
<body>
<div class="top-navbar">
<a class="burger-icon" onclick="toggleMenu()">
<svg name="Hamburger" style="vertical-align: middle;" width="34" height="34" viewbox="0 0 24 24"><path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" d="M20 6H4V9H20V6ZM4 10.999H20V13.999H4V10.999ZM4 15.999H20V18.999H4V15.999Z"></path></svg>
</a>


<a class="brand" href="../index.html">
<img src="../https://github.com/ToniMacaroni/RedLoader/blob/cd2308cbe9b7b8c68eb6217ecb557ab417479208/Resources/redlogo.png?raw=true" alt="" class="logomark">
<span class="brand-title"></span>
</a> </div>

<div class="body-content">
<div id="blackout" class="blackout" onclick="toggleMenu()"></div>

<nav id="sidebar" role="navigation">
<div class="sidebar">

<div>
<div class="mobile-hide">

<a class="brand" href="../index.html">
<img src="../https://github.com/ToniMacaroni/RedLoader/blob/cd2308cbe9b7b8c68eb6217ecb557ab417479208/Resources/redlogo.png?raw=true" alt="" class="logomark">
<span class="brand-title"></span>
</a> </div>

<div class="sidesearch">
<form id="search" role="search" class="search">
<i class="bi bi-search search-icon"></i>
<input type="text" id="search-query" placeholder="Search" autocomplete="off">
</form>
</div>

<div id="navbar">
</div>
</div> <div class="sidebar-item-separator"></div>

<div id="sidetoggle">
<div id="sidetoc"></div>
</div>
</div>
</nav>

<main class="main-panel">

<div id="search-results" style="display: none;">
<h1 class="search-list">Search Results for <span></span></h1>
<div class="sr-items">
<p><i class="bi bi-hourglass-split index-loading"></i></p>
</div>
<ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
</div>


<div role="main" class="hide-when-search">

<div class="subnav navbar navbar-default">
<div class="container hide-when-search" id="breadcrumb">
<ul class="breadcrumb">
<li></li>
</ul>
</div>
</div>


<article class="content wrap" id="_content" data-uid="">
<h1 id="adding-custom-data-to-a-save-file">Adding custom data to a save file</h1>

<p>Red Loader has utilities if you want your mod to persist data in save games.
This is useful if you want to save settings or other data that you want to persist between game sessions for a particular save.</p>
<h2 id="serializer-class">Serializer class</h2>
<p>Your mod needs some kind of class to serialize and deserialize your data.
A common way is to have some kind of &quot;manager&quot; class that holds all the data you want to save.</p>
<pre><code class="lang-csharp">public class MyModManager : ICustomSaveable&lt;MyModManager.MyModSaveData&gt;
{
public string Name =&gt; &quot;MyModManager&quot;;

// Used to determine if the data should also be saved in multiplayer saves
public bool IncludeInPlayerSave =&gt; true;

public MyModSaveData Save()
{
// Serialize your data from game state here
return new MyModSaveData();
}

public void Load(MyModSaveData obj)
{
// Apply game state from your data here
}

public class MyModSaveData
{
public Color SomeColor;
public Vector3 SomeVector;
}
}
</code></pre>
<p>The important parts here are the <code>ICustomSaveable</code> interface that <code>MyModManager</code> implements, and the <code>MyModSaveData</code> class that is used to serialize and deserialize the data.
The <code>Save</code> and <code>Load</code> methods from the <code>ICustomSaveable</code> interface are used to serialize and deserialize the data.
Red Loader will call them when a save game gets loaded or the user requests a save.</p>
<h2 id="registering-your-serializer">Registering your serializer</h2>
<p>Registering the serializer is as easy as calling the following in <code>OnSdkInitialized</code>:</p>
<pre><code class="lang-csharp">var manager = new MyModManager();
SonsSaveTools.Register(manager);
</code></pre>
</article>
</div>

</main>
</div>


<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script type="text/javascript" src="../styles/jquery.twbsPagination.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.mark.min.js"></script>
<script type="text/javascript" src="../styles/url.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/anchor.min.js"></script>
<script type="text/javascript" src="../styles/docfx.js"></script>
<script type="text/javascript" src="../styles/main.js"></script>
<script type="text/javascript" src="../styles/singulink.js"></script> </body>
</html>
4 changes: 4 additions & 0 deletions docs/articles/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<li>
<a href="bundles.html" class="sidebar-item" name="" title="Asset Bundles and Addressables">Asset Bundles and Addressables</a>

</li>
<li>
<a href="custom-save-data.html" class="sidebar-item" name="" title="Custom Save Data">Custom Save Data</a>

</li>
<li>
<a href="freecam.html" class="sidebar-item" name="" title="Freecam">Freecam</a>
Expand Down
5 changes: 5 additions & 0 deletions docs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,11 @@
"title": "Creating Mods | RedLoader Docs",
"keywords": "Creating Mods This article will cover how to setup a mod project using the RedManager as it's the easiest way. If you want to use the template directory you can see it here. Creating the project (RedManager) Start up the RedManager If you path isn't correctly set, adjust it Head over to the Modders tab Click on Install template if you haven't installed the template yet Put in your mod name and click Create Project Select the target folder for your mod (a subfolder with your mod name will be created in the target folder) Creating the project (CLI) If you want to use the command line to create your project look at the readme here. IMPORTANT The RedManager can't update the template once a new one comes out. If you want to update the template open up the console and execute dotnet new install RedLoader.Templates. Project description The project includes several features out of the box: The mod will automatically be copied to the game on build If you use Rider you can press Ctrl+F5 to automatically copy the mod and run the game If you set the mode to release it will: Automatically create a folder with the correct mod structure Automatically create a zip with the created folder The template also sets up a basic mod for you. You should be able to just build the mod and see the mod being loaded. Adjusting the mod info The first thing you should do is adjust your mod info (like author, mod name etc.) in the manifest.json file. The file is linked to a valid json schema, so if you are ever unsure what you can write in there just press Ctrl+Space to open autocomplete. Tips If you want to automatically apply harmony patches for your assembly add HarmonyPatchAll = true; to your constructor (or uncomment it). If you want to subscribe to unity's OnUpdate, OnFixedUpdate, OnGUI and OnLateUpdate message queue you can register it in the constructor by for example using OnUpdateCallback = MyMethod. To reference additional assemblies, add them from the _RedLoader/Game folder. Non game assemblies like harmony can be referenced from the _RedLoader/net6 folder."
},
"articles/custom-save-data.html": {
"href": "articles/custom-save-data.html",
"title": "Adding custom data to a save file | RedLoader Docs",
"keywords": "Adding custom data to a save file Red Loader has utilities if you want your mod to persist data in save games. This is useful if you want to save settings or other data that you want to persist between game sessions for a particular save. Serializer class Your mod needs some kind of class to serialize and deserialize your data. A common way is to have some kind of \"manager\" class that holds all the data you want to save. public class MyModManager : ICustomSaveable<MyModManager.MyModSaveData> { public string Name => \"MyModManager\"; // Used to determine if the data should also be saved in multiplayer saves public bool IncludeInPlayerSave => true; public MyModSaveData Save() { // Serialize your data from game state here return new MyModSaveData(); } public void Load(MyModSaveData obj) { // Apply game state from your data here } public class MyModSaveData { public Color SomeColor; public Vector3 SomeVector; } } The important parts here are the ICustomSaveable interface that MyModManager implements, and the MyModSaveData class that is used to serialize and deserialize the data. The Save and Load methods from the ICustomSaveable interface are used to serialize and deserialize the data. Red Loader will call them when a save game gets loaded or the user requests a save. Registering your serializer Registering the serializer is as easy as calling the following in OnSdkInitialized: var manager = new MyModManager(); SonsSaveTools.Register(manager);"
},
"articles/freecam.html": {
"href": "articles/freecam.html",
"title": "Freecam | RedLoader Docs",
Expand Down
10 changes: 10 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,16 @@
},
"version": ""
},
{
"type": "Conceptual",
"source_relative_path": "articles/custom-save-data.md",
"output": {
".html": {
"relative_path": "articles/custom-save-data.html"
}
},
"version": ""
},
{
"type": "Conceptual",
"source_relative_path": "articles/freecam.md",
Expand Down

0 comments on commit 2aaf064

Please sign in to comment.