Skip to content

Commit b0c2243

Browse files
committed
Back to using async, works with no lag
1 parent 30494a1 commit b0c2243

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

BotCommands.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BepInEx;
22
using BepInEx.Configuration;
33
using System.IO;
4+
using System.Threading.Tasks;
45
using UnityEngine;
56

67
namespace BotCMDs
@@ -11,11 +12,10 @@ public class BotCommands : BaseUnityPlugin
1112
{
1213
// Config
1314
private static ConfigEntry<string> Cmdpath { get; set; }
14-
1515
private string botcmd_path;
1616

1717
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members")]
18-
public void Awake()
18+
private void Awake()
1919
{
2020
Cmdpath = Config.Bind<string>(
2121
"Config",
@@ -26,26 +26,50 @@ public void Awake()
2626
botcmd_path = Cmdpath.Value;
2727

2828
Debug.Log("Created by Rayss and InfernalPlacebo.");
29+
30+
}
31+
32+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members")]
33+
private void Start()
34+
{
35+
Reading();
2936
}
3037

38+
/**
3139
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members")]
3240
private void Update()
3341
{
34-
using (StreamReader reader = new StreamReader(new FileStream(botcmd_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
42+
43+
}
44+
**/
45+
46+
private async void Reading()
47+
{
48+
using (StreamReader reader = new StreamReader(new FileStream(botcmd_path,
49+
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
3550
{
3651
//start at the end of the file
3752
long lastMaxOffset = reader.BaseStream.Length;
3853

39-
System.Threading.Thread.Sleep(20);
54+
while (true)
55+
{
56+
await Task.Delay(1000);
57+
58+
//if the file size has not changed, idle (RAYSS NOTE: MAY NOT BE NEEDED)
59+
if (reader.BaseStream.Length == lastMaxOffset)
60+
continue;
4061

41-
//seek to the last max offset
42-
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
62+
//seek to the last max offset
63+
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
4364

44-
//read out of the file until the EOF
45-
string line = "";
65+
//read out of the file until the EOF
66+
string line = "";
67+
while ((line = reader.ReadLine()) != null)
68+
RoR2.Console.instance.SubmitCmd(null, line);
4669

47-
while ((line = reader.ReadLine()) != null)
48-
RoR2.Console.instance.SubmitCmd(null, line);
70+
//update the last max offset
71+
lastMaxOffset = reader.BaseStream.Position;
72+
}
4973
}
5074
}
5175
}

0 commit comments

Comments
 (0)