Skip to content

Commit 19a9bf6

Browse files
committed
Fix tag parsing; add process filter
1 parent b9e6026 commit 19a9bf6

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

ScriptLoader/ScriptInfo.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using UnityEngine;
45

56
namespace ScriptLoader
67
{
@@ -12,20 +13,22 @@ internal class ScriptInfo
1213
["name"] = (si, c) => si.Name = c,
1314
["author"] = (si, c) => si.Author = c,
1415
["desc"] = (si, c) => si.Description = c,
15-
["ref"] = (si, c) => si.References.Add(c.Template(Utilities.KnownPaths))
16+
["ref"] = (si, c) => si.References.Add(c.Template(Utilities.KnownPaths)),
17+
["proc_filter"] = (si, c) => si.ProcessFilters.Add(c)
1618
};
1719

1820
public string Author { get; set; }
1921
public string Name { get; set; }
2022
public string Description { get; set; }
2123
public List<string> References { get; set; } = new List<string>();
24+
public List<string> ProcessFilters { get; set; } = new List<string>();
2225

2326
public static ScriptInfo FromTextFile(string path)
2427
{
2528
using (var tw = File.OpenText(path))
2629
{
2730
string line;
28-
ScriptInfo si = null;
31+
var si = new ScriptInfo();
2932
while ((line = tw.ReadLine()) != null)
3033
{
3134
line = line.Trim();
@@ -41,11 +44,9 @@ public static ScriptInfo FromTextFile(string path)
4144
if (nextSpace < 0)
4245
continue;
4346

44-
var command = line.Substring(1, nextSpace);
47+
var command = line.Substring(1, nextSpace - 1);
4548
var value = line.Substring(nextSpace).Trim();
4649

47-
if (si == null)
48-
si = new ScriptInfo();
4950
if (CommandParsers.TryGetValue(command, out var parser))
5051
parser(si, value);
5152
}

ScriptLoader/ScriptLoader.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace ScriptLoader
1212
{
13-
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.2")]
13+
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.2.1")]
1414
public class ScriptLoader : BaseUnityPlugin
1515
{
1616
private readonly string scriptsPath = Path.Combine(Paths.GameRootPath, "scripts");
@@ -82,11 +82,21 @@ private void CompileScripts()
8282
if (!File.Exists(ignoresPath))
8383
File.WriteAllText(ignoresPath, "");
8484

85+
bool IsValidProcess(string scriptFile)
86+
{
87+
var si = availableScripts[scriptFile];
88+
89+
if (si.ProcessFilters.Count == 0)
90+
return true;
91+
return si.ProcessFilters.Any(p => string.Equals(p.ToLowerInvariant().Replace(".exe", ""), Paths.ProcessName,
92+
StringComparison.InvariantCultureIgnoreCase));
93+
}
94+
8595
var ignores = new HashSet<string>(File.ReadAllLines(ignoresPath).Select(s => s.Trim()));
86-
var scriptsToCompile = files.Where(f => !ignores.Contains(Path.GetFileName(f))).ToList();
96+
var scriptsToCompile = files.Where(f => IsValidProcess(f) && !ignores.Contains(Path.GetFileName(f))).ToList();
8797

8898
Logger.LogInfo(
89-
$"Found {files.Length} scripts to compile, skipping {files.Length - scriptsToCompile.Count} scripts because of `scriptignores`");
99+
$"Found {files.Length} scripts to compile, skipping {files.Length - scriptsToCompile.Count} scripts because of `scriptignores` or process filters");
90100

91101
var md5 = MD5.Create();
92102
var scriptDict = new Dictionary<string, byte[]>();
@@ -108,7 +118,7 @@ private void CompileScripts()
108118

109119
foreach (var scriptFile in scriptsToCompile)
110120
{
111-
if (!availableScripts.TryGetValue(scriptFile, out var info) || info == null) continue;
121+
if (!availableScripts.TryGetValue(scriptFile, out var info)) continue;
112122
foreach (var infoReference in info.References)
113123
Assembly.LoadFile(infoReference);
114124
}

0 commit comments

Comments
 (0)