help? 09-25-2015, 06:28 AM
#1
Hey guys, so I've been coding plugins for Unturned, as I'm doing a competition for it, and this is a command / chat logger, but it's client sided, meaning you will just put it in your mods, and you can copy all chat into a .txt file when you open the chat menu/logger.
here is the problem, the game loads fine, but when I press (insert) to open it, it crashes my game.
if you want you can look at the source and tell me what you think...
I hope to get this fixed by tomorrow since the competition is saturday (hopefully if y'all start voting)
here is the problem, the game loads fine, but when I press (insert) to open it, it crashes my game.
if you want you can look at the source and tell me what you think...
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using Steamworks;
using UnityEngine;
namespace Chat_Box
{
public class Main : MonoBehaviour
{
private Vector2 scrollPosition;
private List<String> ChatLog = new List<string>();
private string PreviousChat = "A:B:C";
private string steamName = SteamFriends.GetPersonaName();
private bool chatToggle = true;
private bool autoChat = true;
private string chatMessages;
private string autoChatString;
private String getMessageText()
{
return getChat(6);
}
private String getMessageGroup()
{
return getChat(5);
}
private String getMessageName()
{
return getChat(3);
}
private String getChat(int f)
{
// Getting chat message fields using Reflection BY SIN
NetworkChat[] chat = UnityEngine.Object.FindObjectsOfType(typeof(NetworkChat)) as NetworkChat[];
FieldInfo[] fields = typeof(NetworkChat).GetFields();
int num = 0;
foreach (NetworkChat input in chat)
{
foreach (FieldInfo field in fields)
{
if (num == f)
{
try
{
return (field.GetValue(input).ToString());
}
catch (Exception)
{
}
}
num++;
}
}
return "";
}
private void logChat(string chat)
{
StreamWriter chatlog = new StreamWriter(("Unturned_Data/Managed/mods/ChatLogger/Chatlog.txt"), true);
chatlog.WriteLine("[" + DateTime.Now.ToString() + "]");
chatlog.WriteLine(chat);
chatlog.WriteLine("");
chatlog.Close();
}
private bool getBool()
{
string s = File.ReadAllText("Unturned_Data/Managed/mods/ChatLogger/LogChat.txt");
if (s.ToLower() == "yes")
return true;
else
return false;
}
void Start()
{
Directory.CreateDirectory("Unturned_Data/Managed/mods/ChatLogger");
if (!File.Exists("Unturned_Data/Managed/mods/ChatLogger/LogChat.txt"))
{
File.WriteAllText("Unturned_Data/Managed/mods/ChatLogger/LogChat.txt", "yes");
}
}
void Update()
{
#region Hotkeys
if (Input.GetKey(KeyCode.PageUp))
scrollPosition.y = scrollPosition.y - 10;
if (Input.GetKey(KeyCode.PageDown))
scrollPosition.y = scrollPosition.y + 10;
if (Input.GetKeyDown(KeyCode.Keypad0))
this.chatToggle = !this.chatToggle;
if (Input.GetKeyDown(KeyCode.Delete))
this.autoChat = !this.autoChat;
if (autoChat)
{
//Constantly moves the y positon of the scrollbar to 9999999, Which forces it to the bottom.
scrollPosition.y = 99999999;
autoChatString = "on";
}
else
{
autoChatString = "off";
}
#endregion
#region Chat Messages
//Ensures no Duplicate messages are allowed.
if (getMessageText().Length > 0 && !this.PreviousChat.Equals(string.Concat(getMessageName(), ": ", getMessageText())) && chatToggle)
{
//Re-defines Previous chat for the above If statement
this.PreviousChat = string.Concat(getMessageName(), ": ", getMessageText());
//Checks if logging is enabled
if (getBool())
{
logChat(string.Concat(getMessageName(), ": ", getMessageText()));
}
// Concatenates the messages into one string, to reduce lag.
chatMessages += "\n" + string.Concat(getMessageName(), ": ", getMessageText());
// Checks that the message is from the player, and its contents.
if (getMessageName() == steamName)
{
if (getMessageText().ToLower().Equals("/clear"))
{
chatMessages = "";
}
}
}
#endregion
}
void OnGUI()
{
if (chatToggle)
{
//Using AutoLayout so that its perfectly placed regardless of Screen.width/height
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUI.color = Color.grey;
GUILayout.Label("Scrollable Chat Box v1.2 \n Auto Scroll: " + autoChatString);
GUI.color = Color.white;
this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, GUILayout.Height(230), GUILayout.Width(380));
GUI.color = Color.cyan;
//Looks neater than having it centered.
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUILayout.Label(chatMessages);
//Returned to center so that the Title doesn't get moved after 1 tick.
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.color = Color.white;
GUILayout.EndScrollView();
GUILayout.Space(-5);
GUILayout.Box("", GUILayout.Height(-230), GUILayout.Width(380));
GUILayout.EndVertical();
GUILayout.Space(50);
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
GUILayout.EndArea();
}
}
}
}I hope to get this fixed by tomorrow since the competition is saturday (hopefully if y'all start voting)
Devon | Kik: Hurry




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)