-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile readerModule.cs
More file actions
98 lines (88 loc) · 2.86 KB
/
File readerModule.cs
File metadata and controls
98 lines (88 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using VRCOSC.Game.Modules;
using VRCOSC.Game.Modules.Avatar;
namespace VRCOSC.Modules.FileReader;
[ModuleTitle("File reader")]
[ModuleDescription("Reads the contents of a text file")]
[ModuleAuthor("YukiyaVR", "https://github.com/YukiyaVR")]
[ModuleGroup(ModuleType.General)]
public sealed class FileReaderModule : ChatBoxModule
{
private string? filePath;
private string? fileContent;
protected override void CreateAttributes()
{
CreateSetting(FileReaderSetting.FilePath, "File Path", "The path to the text file", string.Empty);
CreateVariable(FileReaderVariable.FileContent, "File Content", "content");
CreateState(FileReaderState.Default, "Default", $"{GetVariableFormat(FileReaderVariable.FileContent)}");
CreateEvent(FileVariableChanged.TextUpdate, "Content Changed", $"{GetVariableFormat(FileReaderVariable.FileContent)}", 4);
}
protected override void OnModuleStart()
{
ChangeStateTo(FileReaderState.Default);
filePath = GetSetting<string>(FileReaderSetting.FilePath);
if (IsValidFilePath())
{
Log("File path is valid. Reading file content...");
}
else
{
Log("Invalid file path. Please provide a valid file path in the module settings.");
}
}
[ModuleUpdate(ModuleUpdateMode.ChatBox)]
private void updateVariables()
{
if (IsValidFilePath())
{
ReadFileContent();
}
}
private bool IsValidFilePath()
{
return !string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath);
}
private void ReadFileContent()
{
try
{
if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
{
string newFileContent = System.IO.File.ReadAllText(filePath);
if (newFileContent != fileContent)
{
TriggerEvent(FileVariableChanged.TextUpdate);
fileContent = newFileContent;
SetVariableValue(FileReaderVariable.FileContent, fileContent);
}
}
else
{
fileContent = string.Empty;
SetVariableValue(FileReaderVariable.FileContent, fileContent);
}
}
catch (Exception ex)
{
Log($"Error reading file: {ex.Message}");
fileContent = string.Empty;
SetVariableValue(FileReaderVariable.FileContent, fileContent);
}
}
private enum FileReaderSetting
{
FilePath
}
private enum FileReaderState
{
Default
}
private enum FileReaderVariable
{
FileContent
}
private enum FileVariableChanged
{
TextUpdate
}
}