Skip to content

Commit

Permalink
Cleanup, removed loading of pre set first script
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-fa committed Jan 30, 2024
1 parent 0ab85cf commit 1dcfe5c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 58 deletions.
15 changes: 0 additions & 15 deletions PAWN/a_dcamx.inc
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,6 @@ native Unloadscript(script[]);
native CallRemoteFunction(function[], format[], {Float,_}:...);
native DC_GetBotPing();

// - INIFile (DCAMX core)
native INI_Delete(fileid);
native INI_Open(filepath[]);
native INI_Close(fileid);
native INI_Write(fileid, key[], value[], section[]);
native INI_WriteInt(fileid, key[], value, section[]);
native Float:INI_ReadFloat(fileid, key[], section[]);
native INI_WriteFloat(fileid, key[], Float:value, section[]);
native INI_Read(fileid, key[], section[], output[]);
native INI_ReadInt(fileid, key[], section[]);
native INI_KeyExists(fileid, key[], section[]);
native INI_Exists(filepath[]);
native INI_DeleteSection(fileid, section[]);
native INI_DeleteKey(fileid, key[], section[]);

// - Discord / Bot
native DC_SetMinLogLevel(lvl);
native DC_SetActivityText(text[], activity_type = DISCORD_ACTIVITY_PLAYING);
Expand Down
34 changes: 19 additions & 15 deletions Scripting/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,44 @@


//Note
//Unload/Reload should check for
//load/Reload should check for
//scriptfile hash Differences because of name problems
namespace discordamx.Scripting
{
static class Manager
{
private static bool m_Inited;
public static List<Script> m_Scripts = new List<Script>();
public static string m_InitScriptName = null!;
public static Script m_InitScript = null!;

public static void LoadFiles()
{

Script scr = null!;
byte[] _hash = null!;
int first = 0;
foreach (string x in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"\Scripts\"))
{
if (!x.Contains(".amx")) continue;
if (x.Equals(m_InitScript)) continue; //the main file is reserved to be loaded first.
try
{
scr = new Script(x);

m_Scripts.Add(scr!);

using (var md5 = MD5.Create())

//Pregen the hash, make sure its not equal to the main script already loaded.
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(x))
{
using (var stream = File.OpenRead(x))
{
scr.m_Hash = md5.ComputeHash(stream);
}
_hash = md5.ComputeHash(stream);
}
}

Log.Debug("Loaded Script + " + x + " with hash = " + BitConverter.ToString(scr.m_Hash).Replace("-", "").ToLowerInvariant() + "!");
try
{
scr = new Script(x);
scr.m_Hash = _hash;
m_Scripts.Add(scr);
Log.Debug("Loaded Script " + x + " with hash = " + BitConverter.ToString(scr.m_Hash).Replace("-", "").ToLowerInvariant() + "!");
if (first == 0) scr.m_Amx.ExecuteMain(); //Only for the first file.

first = 1;
var p = scr.m_Amx.FindPublic("OnInit");
if (p != null) p.Execute();
}
Expand Down
2 changes: 1 addition & 1 deletion Scripting/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace discordamx.Scripting
public class Script
{
public string m_amxFile = null!;
public AMX m_Amx;
public AMX m_Amx = null!;
public byte[] m_Hash = null!;
//public static List<Scripting.ScriptTimer> m_ScriptTimers = null!;

Expand Down
45 changes: 18 additions & 27 deletions Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using discordamx.Discord;
using DSharpPlus.Entities;
using discordamx.Utils;
using discordamx.Scripting;

namespace discordamx
{
Expand Down Expand Up @@ -82,6 +83,10 @@ static void Setup(string[] args)
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Plugins/"))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Plugins/");





//check if main config exists..
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "discordamx.properties"))
{
Expand All @@ -98,47 +103,33 @@ static void Setup(string[] args)
Program.dConfig.Token = m_Properties.get("discord-bot-token");

//Start the discord bot
Discord.Bot.RunAsync(dConfig).GetAwaiter().GetResult();
Bot.RunAsync(dConfig).GetAwaiter().GetResult();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[INFO] Waiting for first guild data download to finish..");
Log.Debug("Waiting for first guild data download to finish..");
while (!Program.m_ScriptingInited)
{
Console.Write(".");
Thread.Sleep(900);
}
Console.Write("\n");


foreach (string fl in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "Plugins/"))
{
if (!fl.EndsWith(".dll")) continue;
Log.Info("\n---------------------------------------------\n" +
"[CORE] Found plugin: '" + fl + "' !");
Log.Info("\n---------------------------------------------");
new Plugins.Plugin(fl);
}


if (!String.IsNullOrEmpty(m_Properties.get("main-script")) || !String.IsNullOrWhiteSpace(m_Properties.get("main-script")))
{
//Load a specific script first.
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Scripts/" + m_Properties.get("main-script") + ".amx"))
{
Scripting.Manager.m_InitScriptName = m_Properties.get("main-script");
Scripting.Manager.m_InitScript = new Scripting.Script(AppDomain.CurrentDomain.BaseDirectory + "Scripts/" + m_Properties.get("main-script") + ".amx");
Scripting.Manager.m_InitScript.m_Amx.ExecuteMain();
}
}


}
else
{
StopEverything();
return;
}


//Load the plugins
/*foreach (string fl in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "Plugins/"))
{
if (!fl.EndsWith(".dll")) continue;
Log.Info("---------------------------------------------\n" +
"[CORE] Found plugin: '" + fl + "' !");
Log.Info("---------------------------------------------");
new Plugins.Plugin(fl);
}*/

/*
int idx = 0;
Expand Down Expand Up @@ -173,7 +164,7 @@ static void Setup(string[] args)
idx++;
}*/


//Load all the other files.
Scripting.Manager.LoadFiles();

Expand Down

0 comments on commit 1dcfe5c

Please sign in to comment.