Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using UnityEngine;
- using spaar.ModLoader;
- using spaar.ModLoader.UI;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace BesiegeMultiplayer
- {
- public class NetworkBlock : MonoBehaviour {
- public int id = 0;
- }
- public class BesiegeModLoader : Mod {
- public override string Name { get { return "BesiegeMultiplayerMod"; } }
- public override string DisplayName { get { return "Multiplayer Mod"; } }
- public override string BesiegeVersion { get { return "v0.10"; } }
- public override string Author { get { return "TesseractCat"; } }
- public override Version Version { get { return new Version(1, 0); } }
- public override bool CanBeUnloaded { get { return true; } }
- public GameObject temp;
- public override void OnLoad()
- {
- GameObject temp = new GameObject ();
- temp.AddComponent<BesiegeMultiplayerMod> ();
- GameObject.DontDestroyOnLoad (temp);
- }
- public override void OnUnload() {
- GameObject.Destroy (temp);
- }
- }
- //[spaar.ModLoader.
- //[spaar.Mod("Besiege Multiplayer Mod",version="0.1",author="TesseractCat")]
- public class BesiegeMultiplayerMod : MonoBehaviour
- {
- void Start() {
- this.gameObject.AddComponent<NetworkView> ();
- //StartCoroutine (Loop ());
- StartCoroutine (Loop2 ());
- }
- public Rect windowRect = new Rect(20,20,250,300);
- public Rect windowRect2 = new Rect(20,20,300,360);
- public Rect windowRect3 = new Rect(100,100,250,190);
- public GameObject NetworkManager;
- public string ServerName = "Untitled Server";
- public string ChatTextEntry = "";
- public string Username = "User";
- public string ChatText = "";
- public string IPText = "";
- public string Password = "";
- public List<GameObject> networkBlocks = new List<GameObject> ();
- public bool Connected = false;
- public bool UpdateInstant = true;
- public bool PasswordEntry = false;
- public bool PasswordEntryMode = false;
- public Vector2 scrollPos;
- public Vector2 scrollPos2;
- public HostData elementtoconnect;
- Dictionary<string,int> blockTypeValues = new Dictionary<string,int>();
- public void setUp() {
- Transform[] list = spaar.ModLoader.Game.AddPiece.blockTypes;
- for(int i = 0; i<list.Length; i++){
- if(list[i]!=null)
- blockTypeValues[list[i].GetComponent<MyBlockInfo>().blockName] = i;
- }
- }
- void OnGUI() {
- try {
- GUI.skin = ModGUI.Skin;
- } catch { }
- windowRect = GUI.Window (-2001, windowRect, DoMyWindow, "Multiplayer Settings");
- windowRect2 = GUI.Window (-2002, windowRect2, DoMyWindow2, "Multiplayer Chat");
- if (PasswordEntry) {
- windowRect3 = GUI.Window (-2003, windowRect3, DoMyWindow3, "Password?");
- }
- }
- void DoMyWindow(int windowID) {
- GUILayout.BeginHorizontal ();
- if (GUILayout.Button ("Create Server")) {
- //Network.incomingPassword = Password;
- //PasswordEntry=true;
- //PasswordEntryMode = false;
- Network.InitializeServer (1, 25002, !Network.HavePublicAddress ());
- MasterServer.RegisterHost ("Besiege Server", ServerName);
- Connected = true;
- }
- ServerName = GUILayout.TextField (ServerName);
- /**GUILayout.EndHorizontal ();
- GUILayout.BeginHorizontal ();
- if (GUILayout.Button ("Connect")) {
- Network.Connect (IPText, 25002);
- }**/
- //GUILayout.Label ("IP: ");
- //IPText = GUILayout.TextField (IPText);
- GUILayout.EndHorizontal ();
- /**GUILayout.BeginHorizontal ();
- GUILayout.Label ("Password: ");
- Password = GUILayout.TextField (Password);
- GUILayout.EndHorizontal ();**/
- if (GUILayout.Button ("Disconnect From All Servers")) {
- Network.Disconnect ();
- Connected = false;
- }
- /**GUILayout.BeginHorizontal ();
- GUILayout.EndHorizontal ();**/
- scrollPos2 = GUILayout.BeginScrollView (scrollPos2);
- GUILayout.BeginVertical ("Box");
- MasterServer.RequestHostList ("Besiege Server");
- HostData[] data = MasterServer.PollHostList ();
- foreach (var element in data)
- {
- GUILayout.BeginHorizontal();
- var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
- GUILayout.Label(name);
- //GUILayout.Space(5);
- //string hostInfo;
- //hostInfo = "[";
- //foreach (var host in element.ip)
- // hostInfo = hostInfo + host + ":" + element.port + " ";
- //hostInfo = hostInfo + "]";
- //GUILayout.Label(hostInfo);
- //GUILayout.Space(5);
- //GUILayout.Label(element.comment);
- //GUILayout.Space(5);
- //GUILayout.FlexibleSpace();
- if (GUILayout.Button("Connect"))
- {
- // Connect to HostData struct, internally the correct method is used (GUID when using NAT).
- Network.Connect(element);
- Connected = true;
- //elementtoconnect = element;
- //PasswordEntry=true;
- //PasswordEntryMode = true;
- }
- GUILayout.EndHorizontal();
- }
- GUILayout.EndVertical ();
- GUILayout.EndScrollView ();
- GUI.DragWindow ();
- }
- void DoMyWindow2 (int windowID) {
- scrollPos = GUILayout.BeginScrollView (scrollPos,false,false);
- //GUI.TextArea (new Rect (10, 10, 230, 240),ChatText);
- GUILayout.TextArea (ChatText);
- GUILayout.EndScrollView ();
- GUILayout.BeginHorizontal ();
- if (Event.current.Equals( Event.KeyboardEvent ("return"))) {
- if (Connected) {
- networkView.RPC ("PostChat", RPCMode.All, Username+": "+ChatTextEntry);
- ChatTextEntry = "";
- }
- }
- ChatTextEntry = GUILayout.TextField (ChatTextEntry);
- if (GUILayout.Button ("POST")) {
- if (Connected) {
- networkView.RPC ("PostChat", RPCMode.All, Username+": "+ChatTextEntry);
- ChatTextEntry = "";
- }
- //PostChat(ChatTextEntry);
- }
- GUILayout.EndHorizontal ();
- GUILayout.BeginHorizontal ();
- GUILayout.Label ("Username: ");
- Username = GUILayout.TextField (Username);
- GUILayout.EndHorizontal ();
- GUI.DragWindow ();
- }
- void DoMyWindow3 (int windowID) {
- GUILayout.Label ("Leave blank if there is no password.\n");
- GUILayout.BeginHorizontal ();
- GUILayout.Label ("Password: ");
- Password = GUILayout.TextField (Password);
- GUILayout.EndHorizontal ();
- GUILayout.Label ("\n");
- if (GUILayout.Button ("Done")) {
- PasswordEntry = false;
- if (!PasswordEntryMode) {
- Network.incomingPassword = Password;
- Network.InitializeServer (1, 25002, !Network.HavePublicAddress ());
- MasterServer.RegisterHost ("Besiege Server", ServerName);
- Connected = true;
- } else {
- try {
- Network.Connect(elementtoconnect,Password);
- Connected = true;
- } catch {
- Debug.Log ("An error occured on connection, most likely you didn't enter the correct password.");
- }
- }
- }
- GUI.DragWindow ();
- }
- [RPC]
- void PostChat(string Text) {
- ChatText += Text + "\n";
- }
- [RPC]
- void MassSuicide() {
- foreach (NetworkBlock networkBlock in FindObjectsOfType<NetworkBlock>() as NetworkBlock[]) {
- Destroy(networkBlock.gameObject);
- }
- }
- [RPC]
- void NetInstant(Vector3 g,Quaternion r,int id, int bid) {
- GameObject temp;
- if (id == -1) {
- temp = (GameObject)Instantiate (GameObject.Find("bgeL0").gameObject, g, r);
- } else {
- temp = (GameObject)Instantiate (FindObjectOfType<AddPiece> ().blockTypes [id].gameObject, g, r);
- }
- try {
- Destroy (temp.transform.FindChild ("KeyMapVis").gameObject);
- } catch {
- }
- try {
- Destroy (temp.transform.FindChild ("DirectionArrow").gameObject);
- } catch {
- }
- Destroy(temp.GetComponent<MyBlockInfo> ());
- temp.AddComponent<NetworkBlock> ();
- temp.GetComponent<NetworkBlock> ().id = bid;
- networkBlocks.Add (temp);
- }
- [RPC]
- void NetUpdate(byte[] x, byte[] y, byte[] z, byte[] qx, byte[] qy, byte[] qz, byte[] qw, byte[] id) {
- BinaryFormatter tempBinaryFormatter = new BinaryFormatter ();
- MemoryStream tempXStream = new MemoryStream (x);
- List<float> tempXArray = (List<float>) tempBinaryFormatter.Deserialize (tempXStream);
- MemoryStream tempYStream = new MemoryStream (y);
- List<float> tempYArray = (List<float>) tempBinaryFormatter.Deserialize (tempYStream);
- MemoryStream tempZStream = new MemoryStream (z);
- List<float> tempZArray = (List<float>) tempBinaryFormatter.Deserialize (tempZStream);
- //
- MemoryStream tempQXStream = new MemoryStream (qx);
- List<float> tempQXArray = (List<float>) tempBinaryFormatter.Deserialize (tempQXStream);
- MemoryStream tempQYStream = new MemoryStream (qy);
- List<float> tempQYArray = (List<float>) tempBinaryFormatter.Deserialize (tempQYStream);
- MemoryStream tempQZStream = new MemoryStream (qz);
- List<float> tempQZArray = (List<float>) tempBinaryFormatter.Deserialize (tempQZStream);
- MemoryStream tempQWStream = new MemoryStream (qw);
- List<float> tempQWArray = (List<float>) tempBinaryFormatter.Deserialize (tempQWStream);
- //
- MemoryStream tempIntStream = new MemoryStream (id);
- List<int> tempIntArray = (List<int>) tempBinaryFormatter.Deserialize (tempIntStream);
- //Debug.Log ("TEST");
- foreach (NetworkBlock b in FindObjectsOfType<NetworkBlock>() as NetworkBlock[]) {
- for (int i = 0;i<tempIntArray.Count;i++) {
- if (b.id == tempIntArray[i]) {
- //Debug.Log ("TEST2");
- b.transform.position = new Vector3 (tempXArray [i], tempYArray [i], tempZArray [i]);
- b.transform.rotation = new Quaternion (tempQXArray [i], tempQYArray [i], tempQZArray [i], tempQWArray [i]);
- }
- }
- }
- }
- void NetUpdateLoop() {
- networkView.RPC ("MassSuicide", RPCMode.Others);
- int bi = 0;
- foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
- int i = 0;
- foreach (Transform Block2 in FindObjectOfType<AddPiece>().blockTypes) {
- try {
- if (Block2.GetComponent<MyBlockInfo> ().blockName == Block.blockName) {
- networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, i, bi);
- }
- } catch {
- }
- i++;
- }
- if (Block.gameObject.name == "bgeL0" || Block.gameObject.name == "StartingBlock") {
- networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, -1, bi);
- }
- //networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, blockTypeValues[Block.blockName], bi);
- //}
- bi++;
- //yield return new WaitForSeconds (0.001f);
- //yield return new WaitForSeconds (2f);
- }
- }
- IEnumerator<WaitForSeconds> Loop2() {
- if (Connected) {
- if (GameObject.Find ("bgeL0") || GameObject.Find ("StartingBlock")) {
- //Debug.Log ("TEST");
- //VECTOR 3
- List<float> tempXArray = new List<float> ();
- MemoryStream tempXStream = new MemoryStream ();
- List<float> tempYArray = new List<float> ();
- MemoryStream tempYStream = new MemoryStream ();
- List<float> tempZArray = new List<float> ();
- MemoryStream tempZStream = new MemoryStream ();
- // ---
- //QUATERNION
- List<float> tempQXArray = new List<float> ();
- MemoryStream tempQXStream = new MemoryStream ();
- List<float> tempQYArray = new List<float> ();
- MemoryStream tempQYStream = new MemoryStream ();
- List<float> tempQZArray = new List<float> ();
- MemoryStream tempQZStream = new MemoryStream ();
- List<float> tempQWArray = new List<float> ();
- MemoryStream tempQWStream = new MemoryStream ();
- // ---
- List<int> tempIntArray = new List<int> ();
- MemoryStream tempIntStream = new MemoryStream ();
- BinaryFormatter tempBinaryFormatter = new BinaryFormatter ();
- int bi = 0;
- foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
- //networkView.RPC ("NetUpdate", RPCMode.Others, Block.transform.position, Block.transform.rotation, bi);
- //tempVector3Array.Add(Block.transform.position);
- tempXArray.Add(Block.transform.position.x);
- tempYArray.Add(Block.transform.position.y);
- tempZArray.Add(Block.transform.position.z);
- tempQXArray.Add(Block.transform.rotation.x);
- tempQYArray.Add(Block.transform.rotation.y);
- tempQZArray.Add(Block.transform.rotation.z);
- tempQWArray.Add(Block.transform.rotation.w);
- //tempQuaternionArray.Add (Block.transform.rotation);
- tempIntArray.Add (bi);
- bi++;
- }
- tempBinaryFormatter.Serialize (tempXStream, tempXArray);
- tempBinaryFormatter.Serialize (tempYStream, tempYArray);
- tempBinaryFormatter.Serialize (tempZStream, tempZArray);
- tempBinaryFormatter.Serialize (tempQXStream, tempQXArray);
- tempBinaryFormatter.Serialize (tempQYStream, tempQYArray);
- tempBinaryFormatter.Serialize (tempQZStream, tempQZArray);
- tempBinaryFormatter.Serialize (tempQWStream, tempQWArray);
- tempBinaryFormatter.Serialize (tempIntStream, tempIntArray);
- networkView.RPC ("NetUpdate", RPCMode.Others,
- tempXStream.GetBuffer(),
- tempYStream.GetBuffer(),
- tempZStream.GetBuffer(),
- tempQXStream.GetBuffer(),
- tempQYStream.GetBuffer(),
- tempQZStream.GetBuffer(),
- tempQWStream.GetBuffer(),
- tempIntStream.GetBuffer());
- }
- }
- /**if (FindObjectOfType<MachineObjectTracker> () || FindObjectOfType<MachineObjectTracker> ().allBlockPositions.Count != 0) {
- yield return new WaitForSeconds (FindObjectOfType<MachineObjectTracker> ().allBlockPositions.Count / 100);
- } else {
- yield return new WaitForSeconds (0.01f);
- }**/
- yield return new WaitForSeconds (0.06f);
- StartCoroutine (Loop2());
- }
- void Update() {
- if (!AddPiece.isSimulating) {
- UpdateInstant = true;
- }
- if (Connected) {
- if (GameObject.Find ("bgeL0") || GameObject.Find ("StartingBlock")) {
- if (AddPiece.isSimulating && UpdateInstant == true) {
- //setUp ();
- NetUpdateLoop ();
- UpdateInstant = false;
- }
- }
- }
- /**if (Input.GetKeyDown ("d")) {
- //StartCoroutine ("Loop");
- //networkView.RPC ("MassSuicide", RPCMode.Others);
- foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
- /**foreach (Transform Block2 in FindObjectOfType<AddPiece>().blockTypes) {
- try {
- if (Block2.GetComponent<MyBlockInfo>().blockName==Block.blockName&&Block.name!="CopyPasteInfo") {
- //networkView.RPC("Suicide",RPCMode.AllBuffered);
- Vector3 tempPos = Block.transform.position;
- Quaternion tempRot = Block.transform.rotation;
- Destroy(Block.gameObject);
- GameObject temp = (GameObject) Network.Instantiate (Block2, tempPos, tempRot, 0);
- }
- } catch {
- }
- }
- //Network.Instantiate (Block, Block.transform.position, Block.transform.rotation, 0);
- //Destroy(Block.gameObject);
- if (!Block.GetComponent<NetworkBlock> ()) {
- Block.gameObject.AddComponent<NetworkBlock> ();
- Block.gameObject.AddComponent<NetworkView> ();
- }**/
- //networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position);
- //}
- //networkView.RPC ("MassSuicide", RPCMode.Others);
- //Network.Instantiate (FindObjectOfType<AddPiece>().blockTypes[0], new Vector3 (0, 0, 0), new Quaternion (), 0);
- /**Debug.Log(Block.blockName+":");
- for (int i = 0; i < Block.transform.childCount; i++) {
- Debug.Log (Block.transform.GetChild (i).name);
- }
- }
- }**/
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement