Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
aedenthorn committed May 22, 2022
1 parent 76009f4 commit 832631c
Show file tree
Hide file tree
Showing 17 changed files with 721 additions and 223 deletions.
21 changes: 19 additions & 2 deletions CustomFixedDialogue/DialoguePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public static void LocalizedContentManager_LoadString_Postfix(string path, ref s
return;
ReplaceString(path, ref __result);
}
public static void Game1_LoadStringByGender_Postfix(int npcGender, string key, ref string __result)
{
ReplaceString(key, ref __result, null, npcGender);
}

public static void Dialogue_Prefix(Dialogue __instance, ref string masterDialogue, NPC speaker)
{
FixString(speaker, ref masterDialogue);
Expand Down Expand Up @@ -211,7 +216,7 @@ public static void GetSummitDialogue_Patch(string key, ref string __result)
}
}
}
public static void ReplaceString(string path, ref string text, object[] subs = null)
public static void ReplaceString(string path, ref string text, object[] subs = null, int gender = -1)
{
if (text.Contains("\""))
{
Expand Down Expand Up @@ -259,7 +264,7 @@ public static void ReplaceString(string path, ref string text, object[] subs = n
}
else return;

text = "<" + $"{modifiedPath}{substring}" + ">";
text = "<" + $"{modifiedPath}{substring}" + (gender > -1 ? ($"[{gender}]") : "") + ">";
var x = Environment.StackTrace;
SMonitor.Log($"preparing string {original} for replacement: {text}");
}
Expand All @@ -283,6 +288,12 @@ public static bool FixString(NPC speaker, ref string input)
{
var match = pattern1.Match(input);
string key = match.Groups["key"].Value;
int gender = -1;
if (key.EndsWith("]") && int.TryParse(key.Substring(key.Length - 2, 1), out gender))
{
SMonitor.Log($"Got gender tag {gender}");
key = key.Substring(0, key.Length - 3);
}
string[] subs = null;
string substring = "";
if (match.Value.Contains("`"))
Expand Down Expand Up @@ -327,6 +338,12 @@ public static bool FixString(NPC speaker, ref string input)
}
dontFix = false;
}
if(gender > -1 && newString.Contains("/"))
{
var split = newString.Split('/');
newString = split[gender - 1];
SMonitor.Log($"took gendered string {newString}, gender {gender}");
}
input = input.Replace(match.Value, newString);
SMonitor.Log($"Final replacement for {match.Value}: {newString}.\nCurrent output: {input}");
changed = true;
Expand Down
226 changes: 6 additions & 220 deletions CustomFixedDialogue/ModEntry.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CustomFixedDialogue/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Name": "Custom Fixed Dialogue",
"Author": "aedenthorn",
"Version": "2.1.6",
"Version": "2.1.7",
"Description": "Custom fixed dialogue.",
"UniqueID": "aedenthorn.CustomFixedDialogue",
"EntryDll": "CustomFixedDialogue.dll",
Expand Down
55 changes: 55 additions & 0 deletions DialogueNewLine/CodePatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using HarmonyLib;
using StardewValley.BellsAndWhistles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace DialogueNewLine
{
public partial class ModEntry
{
private static char specialCharacter = '¶';
private static bool newLine;

[HarmonyPatch(typeof(SpriteText), nameof(SpriteText.drawString))]
public class SpriteText_drawString_Patch
{
public static void Prefix()
{
newLine = false;
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
SMonitor.Log($"Transpiling SpriteText.drawString");

var codes = new List<CodeInstruction>(instructions);
for (int i = 0; i < codes.Count; i++)
{
if (i < codes.Count - 1 && codes[i].opcode == OpCodes.Call && typeof(MethodInfo).IsAssignableFrom(codes[i].operand.GetType()) && (MethodInfo)codes[i].operand == AccessTools.Method(typeof(SpriteText), nameof(SpriteText.positionOfNextSpace)))
{
SMonitor.Log("Adding check for special newline character");
codes[i].operand = AccessTools.Method(typeof(ModEntry), nameof(ModEntry.positionOfNextSpace));
}
}

return codes.AsEnumerable();
}
}

private static int positionOfNextSpace(string s, int i, int x, int space)
{
if (!Config.EnableMod)
return SpriteText.positionOfNextSpace(s, i, x, space);
if (newLine)
{
newLine = false;
return int.MaxValue;
}
if(s[i] == specialCharacter)
newLine = true;
return SpriteText.positionOfNextSpace(s, i, x, space);
}
}
}
17 changes: 17 additions & 0 deletions DialogueNewLine/DialogueNewLine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.0</Version>
<TargetFramework>net5.0</TargetFramework>
<EnableHarmony>true</EnableHarmony>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pathoschild.Stardew.ModBuildConfig" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="manifest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions DialogueNewLine/IGenericModConfigMenuApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using StardewModdingAPI;
using System;

namespace DialogueNewLine
{
/// <summary>The API which lets other mods add a config UI through Generic Mod Config Menu.</summary>
public interface IGenericModConfigMenuApi
{
/*********
** Methods
*********/
/****
** Must be called first
****/
/// <summary>Register a mod whose config can be edited through the UI.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="reset">Reset the mod's config to its default values.</param>
/// <param name="save">Save the mod's current config to the <c>config.json</c> file.</param>
/// <param name="titleScreenOnly">Whether the options can only be edited from the title screen.</param>
/// <remarks>Each mod can only be registered once, unless it's deleted via <see cref="Unregister"/> before calling this again.</remarks>
void Register(IManifest mod, Action reset, Action save, bool titleScreenOnly = false);

/// <summary>Add a section title at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="text">The title text shown in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the title, or <c>null</c> to disable the tooltip.</param>
void AddSectionTitle(IManifest mod, Func<string> text, Func<string> tooltip = null);

/// <summary>Add a key binding at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddKeybind(IManifest mod, Func<SButton> getValue, Action<SButton> setValue, Func<string> name, Func<string> tooltip = null, string fieldId = null);

/// <summary>Add a boolean option at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddBoolOption(IManifest mod, Func<bool> getValue, Action<bool> setValue, Func<string> name, Func<string> tooltip = null, string fieldId = null);


/// <summary>Add an integer option at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="min">The minimum allowed value, or <c>null</c> to allow any.</param>
/// <param name="max">The maximum allowed value, or <c>null</c> to allow any.</param>
/// <param name="interval">The interval of values that can be selected.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddNumberOption(IManifest mod, Func<int> getValue, Action<int> setValue, Func<string> name, Func<string> tooltip = null, int? min = null, int? max = null, int? interval = null, string fieldId = null);

/// <summary>Add a string option at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="allowedValues">The values that can be selected, or <c>null</c> to allow any.</param>
/// <param name="formatAllowedValue">Get the display text to show for a value from <paramref name="allowedValues"/>, or <c>null</c> to show the values as-is.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddTextOption(IManifest mod, Func<string> getValue, Action<string> setValue, Func<string> name, Func<string> tooltip = null, string[] allowedValues = null, Func<string, string> formatAllowedValue = null, string fieldId = null);

/// <summary>Remove a mod from the config UI and delete all its options and pages.</summary>
/// <param name="mod">The mod's manifest.</param>
void Unregister(IManifest mod);
}
}
11 changes: 11 additions & 0 deletions DialogueNewLine/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using Microsoft.Xna.Framework;
using StardewModdingAPI;

namespace DialogueNewLine
{
public class ModConfig
{
public bool EnableMod { get; set; } = true;
}
}
68 changes: 68 additions & 0 deletions DialogueNewLine/ModEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.BellsAndWhistles;
using StardewValley.Locations;
using StardewValley.Objects;
using StardewValley.TerrainFeatures;
using System;
using System.Globalization;
using System.Linq;
using Object = StardewValley.Object;

namespace DialogueNewLine
{
/// <summary>The mod entry point.</summary>
public partial class ModEntry : Mod
{

public static IMonitor SMonitor;
public static IModHelper SHelper;
public static ModConfig Config;
public static ModEntry context;

/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
Config = Helper.ReadConfig<ModConfig>();

context = this;

SMonitor = Monitor;
SHelper = helper;

helper.Events.GameLoop.GameLaunched += GameLoop_GameLaunched;
var harmony = new Harmony(ModManifest.UniqueID);
harmony.PatchAll();
}


private void GameLoop_GameLaunched(object sender, GameLaunchedEventArgs e)
{

// get Generic Mod Config Menu's API (if it's installed)
var configMenu = Helper.ModRegistry.GetApi<IGenericModConfigMenuApi>("spacechase0.GenericModConfigMenu");
if (configMenu is null)
return;

// register mod
configMenu.Register(
mod: ModManifest,
reset: () => Config = new ModConfig(),
save: () => Helper.WriteConfig(Config)
);

configMenu.AddBoolOption(
mod: ModManifest,
name: () => "Mod Enabled",
getValue: () => Config.EnableMod,
setValue: value => Config.EnableMod = value
);
}

}
}
22 changes: 22 additions & 0 deletions DialogueNewLine/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Name": "DialogueNewLine",
"Author": "aedenthorn",
"Version": "0.1.0",
"Description": "Insert newline into dialogue text.",
"UniqueID": "aedenthorn.DialogueNewLine",
"EntryDll": "DialogueNewLine.dll",
"MinimumApiVersion": "3.13.0",
"ModUpdater": {
"Repository": "StardewValleyMods",
"User": "aedenthorn",
"Directory": "_releases",
"ModFolder": "DialogueNewLine"
},
"UpdateKeys": [ "Nexus:" ],
"Dependencies": [
{
"UniqueID": "Platonymous.ModUpdater",
"IsRequired": false
}
]
}
Loading

0 comments on commit 832631c

Please sign in to comment.