Skip to content

Commit

Permalink
publicizer 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPurple6411 committed May 13, 2021
1 parent a4fde43 commit 5ece39d
Show file tree
Hide file tree
Showing 73 changed files with 570 additions and 121 deletions.
312 changes: 310 additions & 2 deletions Bepinex-Publicizer/Bepinex-Publicizer.csproj

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Bepinex-Publicizer/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Bepinex_Publicizer
{
using System;
using System.Collections.Generic;

[Serializable]
public class Config
{
public bool UseWhiteList = true;
public List<string> OutputFolders = new List<string>();
public List<string> NameContainsWhiteList = new List<string>() { "Assembly" };
public List<string> NameContainsBlacklist = new List<string>() { "Unity", "BepInEx", "System", "Mono", "Harmony", "Newtonsoft", "LitJSON", "dnlib" };
}
}
167 changes: 143 additions & 24 deletions Bepinex-Publicizer/Main.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using BepInEx;
using BepInEx.Logging;
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using FieldAttributes = dnlib.DotNet.FieldAttributes;
using MethodAttributes = dnlib.DotNet.MethodAttributes;
using TypeAttributes = dnlib.DotNet.TypeAttributes;

namespace Bepinex_Publicizer
namespace Bepinex_Publicizer
{
using BepInEx;
using BepInEx.Logging;
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using FieldAttributes = dnlib.DotNet.FieldAttributes;
using MethodAttributes = dnlib.DotNet.MethodAttributes;
using TypeAttributes = dnlib.DotNet.TypeAttributes;

[BepInPlugin(GUID, MODNAME, VERSION)]
public class Main : BaseUnityPlugin
{
Expand All @@ -25,6 +26,7 @@ public const string
VERSION = "1.0.0.0";

internal readonly ManualLogSource log;
public Config config { get; private set; }

#endregion

Expand All @@ -35,30 +37,144 @@ public Main()

public void Awake()
{

string configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Config.json");

if(File.Exists(configPath))
{
using(StreamReader reader = new StreamReader(configPath))
{
try
{
string configstring = reader.ReadToEnd().Replace("\\\\", "/").Replace("\\", "/");
config = JsonUtility.FromJson<Config>(configstring);
reader.Close();
}
catch(Exception e)
{
log.LogError($"Failed to read {configPath}");
log.LogError(e);
reader.Close();
return;
}
}
}
else
{
config = new Config();
}

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
if(config.OutputFolders.Count == 0)
{
foreach(Assembly assembly in assemblies)
{
try
{
string path = Path.GetDirectoryName(assembly.Location);
if(path?.EndsWith("Managed") ?? false)
{
config.OutputFolders.Add(Directory.CreateDirectory(Path.Combine(path, "publicized_assemblies")).FullName);
using(StreamWriter writer = new StreamWriter(configPath))
{
try
{
writer.WriteLine(JsonUtility.ToJson(config, true));
writer.Flush();
writer.Close();
}
catch
{
writer.Close();
if(File.Exists(configPath))
File.Delete(configPath);
return;
}
}
break;
}
}
catch { }
}
}

foreach (Assembly assembly in assemblies)
StringBuilder stringBuilder = new StringBuilder(Environment.NewLine);
stringBuilder.AppendLine();
int totalCount = 0;
foreach(string outputPath in config.OutputFolders)
{
if (Path.GetDirectoryName(assembly.Location).Contains("Managed") && assembly.GetName().Name.ToLowerInvariant().StartsWith("assembly"))
if(!Directory.Exists(outputPath))
{
ProcessAssembly(assembly);
log.LogError($"Output folder not found at {outputPath}");
return;
}
stringBuilder.AppendLine($"Publicizing files into {outputPath}");
int count = 0;

foreach(Assembly assembly in assemblies)
{
string assemblyName = assembly.GetName().Name;
if(config.UseWhiteList)
{
foreach(string name in config.NameContainsWhiteList)
{
if(assemblyName.ToLower().Contains(name.ToLower()))
{
try
{
ProcessAssembly(assembly, outputPath, ref count, ref totalCount);
}
catch { }
break;
}
}
}
else
{
bool skip = false;
foreach(string name in config.NameContainsBlacklist)
{
if(assemblyName.ToLower().Contains(name.ToLower()))
{
skip = true;
break;
}
}
if(!skip)
{
try
{
ProcessAssembly(assembly, outputPath, ref count, ref totalCount);
}
catch { }
}
}

}
if(count > 0)
{
stringBuilder.AppendLine($" Files output: {count}");
stringBuilder.AppendLine();
}
}

if(totalCount > 0)
{
stringBuilder.AppendLine($"Total files output: {totalCount}");
log.LogMessage(stringBuilder.ToString());
}
}

private void ProcessAssembly(Assembly assembly)
private void ProcessAssembly(Assembly assembly, string outputPath, ref int count, ref int totalCount)
{
string assemblyPath = assembly.Location;
string filename = assembly.GetName().Name;
string outputPath = Path.Combine(Path.GetDirectoryName(assemblyPath), "publicized_assemblies");
string outputSuffix = "_publicized";

Directory.CreateDirectory(outputPath);

string lastHash = null;
string curHash = ComputeHash(assemblyPath);

string hashPath = Path.Combine(outputPath, $"{filename}{outputSuffix}.hash");
string hashPath = Path.Combine(outputPath, $"{filename}.hash");

if (File.Exists(hashPath))
{
Expand All @@ -70,9 +186,11 @@ private void ProcessAssembly(Assembly assembly)
return;
}

log.LogMessage($"Making a public assembly from {filename}");
RewriteAssembly(assemblyPath).Write($"{Path.Combine(outputPath, filename)}{outputSuffix}.dll");
RewriteAssembly(assemblyPath).Write($"{Path.Combine(outputPath, filename)}.dll");
File.WriteAllText(hashPath, curHash);

count++;
totalCount++;
}

private static string ComputeHash(string assemblyPath)
Expand Down Expand Up @@ -115,6 +233,7 @@ private static ModuleDef RewriteAssembly(string assemblyPath)
{
method.Attributes &= ~MethodAttributes.MemberAccessMask;
method.Attributes |= MethodAttributes.Public;
method.Body = new dnlib.DotNet.Emit.CilBody();
}

List<string> eventNames = new List<string>();
Expand Down
4 changes: 2 additions & 2 deletions Bepinex-Publicizer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
22 changes: 11 additions & 11 deletions Bepinex-TwitchController/Player Events/EventLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using TwitchController.Player_Events.Models;
using UnityEngine;

namespace TwitchController.Player_Events
{
Expand Down Expand Up @@ -93,7 +94,7 @@ public string GetBitCosts()
return message;
}

public void Lookup(string EventText, string perp, string userInput)
public void Lookup(string EventText, string perp)
{
if (EventDictionary.TryGetValue(EventText.Trim(), out EventInfo eventInfo))
{
Expand All @@ -112,27 +113,26 @@ public void Lookup(string EventText, string perp, string userInput)
}
}

public void Lookup(string perp, int bits, string userInput)
public void Lookup(string perp, int bits)
{
KeyValuePair<string, EventInfo> Event = EventDictionary.Where(it => it.Value.BitCost > 0 && it.Value.BitCost <= bits)?.OrderByDescending(it => it.Value.BitCost)?.FirstOrDefault() ?? default;
var Events = EventDictionary.Where(it => it.Value.BitCost > 0 && it.Value.BitCost == bits)?.ToList() ?? new List<KeyValuePair<string, EventInfo>>();
KeyValuePair<string, EventInfo> Event = default(KeyValuePair<string, EventInfo>);
if(Events.Count > 0)
Event = Events[UnityEngine.Random.Range(0, Events.Count - 1)];
else
Event = EventDictionary.Where(it => it.Value.BitCost > 0 && it.Value.BitCost <= bits)?.OrderByDescending(it => it.Value.BitCost)?.FirstOrDefault() ?? default;

if (!Event.Equals(default(KeyValuePair<string, EventInfo>)))
{
switch (Event.Value)
switch(Event.Value)
{
case TimedEventInfo timed:
TimedEventInfo tei = new TimedEventInfo(perp, timed);
//controller._log.LogMessage(Event.Key);
ActionQueue.Add(new KeyValuePair<string, EventInfo>(Event.Key, tei));
controller.timer.AddQueueEvent(Event.Key);
break;
case DataEvent dataEvent:
DataEvent de = new DataEvent(perp, dataEvent, userInput);
ActionQueue.Add(new KeyValuePair<string, EventInfo>(Event.Key, de));
controller.timer.AddQueueEvent(Event.Key);
break;
default:
EventInfo ei = new EventInfo(perp, Event.Value);
//controller._log.LogMessage(Event.Key);
ActionQueue.Add(new KeyValuePair<string, EventInfo>(Event.Key, ei));
controller.timer.AddQueueEvent(Event.Key);
break;
Expand Down
4 changes: 2 additions & 2 deletions Bepinex-TwitchController/TwitchClients/Models/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Message

public string Host { get; set; }

public string TriggerText { get; set; }
public string Channel { get; set; }

public string UserInput { get; set; }
public string TriggerText { get; set; }

}
}
Loading

0 comments on commit 5ece39d

Please sign in to comment.