92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
public class Coder : MonoBehaviour
|
|
{
|
|
private const string Separator = "38d1755df3e62026603";
|
|
private const string Separator2 = "gf78sag678fds076a89";
|
|
#if UNITY_EDITOR
|
|
[ContextMenu("Code a file")]
|
|
public void DoIt() {
|
|
string path = EditorUtility.OpenFilePanel("Select a file", "", "");
|
|
if (!string.IsNullOrEmpty(path)) {
|
|
Debug.Log($"Selected file: {path}");
|
|
string A = path.Split(".")[0];
|
|
if (!Directory.Exists(A+"/Testing/")) {
|
|
Directory.CreateDirectory(A+"/Testing/");
|
|
}
|
|
// HideMessageInBinaryFile(path, "Testing");//, A+"/Testing/"+Path.GetFileName(path));
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Decode a file")]
|
|
public void GetMessageFromFile() {
|
|
string path = EditorUtility.OpenFilePanel("Select a file", "", "");
|
|
Debug.Log(RetrieveMessageFromBinaryFile(path));
|
|
}
|
|
#endif
|
|
public void CodeFile(string FilePath, string User, string ID) { // This was only for testing in Editor, Unused
|
|
string B = FilePath.Split(".")[0];
|
|
if (!Directory.Exists(B+"/Testing/")) {
|
|
Directory.CreateDirectory(B+"/Testing/");
|
|
}
|
|
HideMessageInBinaryFile(FilePath, User, ID);//, B+"/Testing/"+Path.GetFileName(FilePath));
|
|
}
|
|
|
|
public void HideMessageInBinaryFile(string originalPath, string User, string ID) {
|
|
// Debug.Log($"HIding User {User} for File {ID}");
|
|
try {
|
|
byte[] UserBytes = Encoding.UTF8.GetBytes(User);
|
|
byte[] IDBytes = Encoding.UTF8.GetBytes(ID);
|
|
byte[] separatorBytes = Encoding.UTF8.GetBytes(Separator);
|
|
byte[] separatorUserID = Encoding.UTF8.GetBytes(Separator2);
|
|
|
|
using (FileStream outputStream = new(originalPath, FileMode.Append, FileAccess.Write)) {
|
|
outputStream.Write(separatorBytes, 0, separatorBytes.Length);
|
|
outputStream.Write(UserBytes, 0, UserBytes.Length);
|
|
outputStream.Write(separatorUserID, 0, separatorUserID.Length);
|
|
outputStream.Write(IDBytes, 0, IDBytes.Length);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
Debug.LogError($"Failed to hide message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
public string RetrieveMessageFromBinaryFile(string combinedPath) {
|
|
try {
|
|
using (FileStream fileStream = new(combinedPath, FileMode.Open, FileAccess.Read)) {
|
|
byte[] buffer = new byte[4096]; // 4GB max
|
|
int bytesRead;
|
|
StringBuilder fileContent = new();
|
|
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) {
|
|
fileContent.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
|
|
int separatorIndex = fileContent.ToString().LastIndexOf(Separator);
|
|
if (separatorIndex != -1) {
|
|
string afterSeparator = fileContent.ToString()[(separatorIndex + Separator.Length)..];
|
|
int separator2Index = afterSeparator.IndexOf(Separator2);
|
|
if (separator2Index != -1) {
|
|
string user = afterSeparator[..separator2Index];
|
|
string id = afterSeparator[(separator2Index + Separator2.Length)..];
|
|
|
|
return user + "?" + id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.LogError("Separator not found in file.");
|
|
return "Separator not found";
|
|
} catch (Exception e) {
|
|
Debug.LogError($"Failed to retrieve message: {e.Message}");
|
|
Debug.LogError($"Stack Trace: {e.StackTrace}");
|
|
return $"File Path: \n{combinedPath} \nMessage: \n{e.Message} \nTrace: \n{e.StackTrace}";
|
|
}
|
|
}
|
|
}
|