Advertisement
Guest User

Multiplayer Mod

a guest
Aug 10th, 2015
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using UnityEngine;
  8. using spaar.ModLoader;
  9. using spaar.ModLoader.UI;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11.  
  12. namespace BesiegeMultiplayer
  13. {
  14.    
  15.  
  16.     public class NetworkBlock : MonoBehaviour {
  17.         public int id = 0;
  18.     }
  19.  
  20.     public class BesiegeModLoader : Mod {
  21.         public override string Name { get { return "BesiegeMultiplayerMod"; } }
  22.         public override string DisplayName { get { return "Multiplayer Mod"; } }
  23.         public override string BesiegeVersion { get { return "v0.10"; } }
  24.         public override string Author { get { return "TesseractCat"; } }
  25.         public override Version Version { get { return new Version(1, 0); } }
  26.         public override bool CanBeUnloaded { get { return true; } }
  27.  
  28.         public GameObject temp;
  29.  
  30.         public override void OnLoad()
  31.         {
  32.             GameObject temp = new GameObject ();
  33.             temp.AddComponent<BesiegeMultiplayerMod> ();
  34.             GameObject.DontDestroyOnLoad (temp);
  35.         }
  36.         public override void OnUnload() {
  37.             GameObject.Destroy (temp);
  38.         }
  39.     }
  40.  
  41.     //[spaar.ModLoader.
  42.     //[spaar.Mod("Besiege Multiplayer Mod",version="0.1",author="TesseractCat")]
  43.     public class BesiegeMultiplayerMod : MonoBehaviour
  44.         {
  45.  
  46.         void Start() {
  47.             this.gameObject.AddComponent<NetworkView> ();
  48.             //StartCoroutine (Loop ());
  49.             StartCoroutine (Loop2 ());
  50.         }
  51.  
  52.         public Rect windowRect = new Rect(20,20,250,300);
  53.         public Rect windowRect2 = new Rect(20,20,300,360);
  54.         public Rect windowRect3 = new Rect(100,100,250,190);
  55.         public GameObject NetworkManager;
  56.         public string ServerName = "Untitled Server";
  57.         public string ChatTextEntry = "";
  58.         public string Username = "User";
  59.         public string ChatText = "";
  60.         public string IPText = "";
  61.         public string Password = "";
  62.         public List<GameObject> networkBlocks = new List<GameObject> ();
  63.         public bool Connected = false;
  64.         public bool UpdateInstant = true;
  65.         public bool PasswordEntry = false;
  66.         public bool PasswordEntryMode = false;
  67.         public Vector2 scrollPos;
  68.         public Vector2 scrollPos2;
  69.         public HostData elementtoconnect;
  70.  
  71.         Dictionary<string,int> blockTypeValues = new Dictionary<string,int>();
  72.         public void setUp() {
  73.             Transform[] list = spaar.ModLoader.Game.AddPiece.blockTypes;
  74.             for(int i = 0; i<list.Length; i++){
  75.                 if(list[i]!=null)
  76.                     blockTypeValues[list[i].GetComponent<MyBlockInfo>().blockName] = i;
  77.             }
  78.         }
  79.  
  80.         void OnGUI() {
  81.             try {
  82.                 GUI.skin = ModGUI.Skin;
  83.             } catch { }
  84.             windowRect = GUI.Window (-2001, windowRect, DoMyWindow, "Multiplayer Settings");
  85.             windowRect2 = GUI.Window (-2002, windowRect2, DoMyWindow2, "Multiplayer Chat");
  86.             if (PasswordEntry) {
  87.                 windowRect3 = GUI.Window (-2003, windowRect3, DoMyWindow3, "Password?");
  88.             }
  89.         }
  90.  
  91.         void DoMyWindow(int windowID) {
  92.  
  93.             GUILayout.BeginHorizontal ();
  94.  
  95.             if (GUILayout.Button ("Create Server")) {
  96.                 //Network.incomingPassword = Password;
  97.                 //PasswordEntry=true;
  98.                 //PasswordEntryMode = false;
  99.  
  100.                 Network.InitializeServer (1, 25002, !Network.HavePublicAddress ());
  101.                 MasterServer.RegisterHost ("Besiege Server", ServerName);
  102.                 Connected = true;
  103.             }
  104.  
  105.             ServerName = GUILayout.TextField (ServerName);
  106.  
  107.             /**GUILayout.EndHorizontal ();
  108.  
  109.             GUILayout.BeginHorizontal ();
  110.  
  111.             if (GUILayout.Button ("Connect")) {
  112.                 Network.Connect (IPText, 25002);
  113.             }**/
  114.  
  115.             //GUILayout.Label ("IP: ");
  116.  
  117.             //IPText = GUILayout.TextField (IPText);
  118.  
  119.             GUILayout.EndHorizontal ();
  120.  
  121.             /**GUILayout.BeginHorizontal ();
  122.  
  123.             GUILayout.Label ("Password: ");
  124.  
  125.             Password = GUILayout.TextField (Password);
  126.  
  127.             GUILayout.EndHorizontal ();**/
  128.  
  129.             if (GUILayout.Button ("Disconnect From All Servers")) {
  130.                 Network.Disconnect ();
  131.                 Connected = false;
  132.             }
  133.  
  134.             /**GUILayout.BeginHorizontal ();
  135.  
  136.  
  137.  
  138.             GUILayout.EndHorizontal ();**/
  139.  
  140.             scrollPos2 = GUILayout.BeginScrollView (scrollPos2);
  141.  
  142.             GUILayout.BeginVertical ("Box");
  143.  
  144.             MasterServer.RequestHostList ("Besiege Server");
  145.  
  146.             HostData[] data = MasterServer.PollHostList ();
  147.  
  148.             foreach (var element in data)
  149.             {
  150.                 GUILayout.BeginHorizontal();    
  151.                 var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
  152.                 GUILayout.Label(name);  
  153.                 //GUILayout.Space(5);
  154.                 //string hostInfo;
  155.                 //hostInfo = "[";
  156.                 //foreach (var host in element.ip)
  157.                 //  hostInfo = hostInfo + host + ":" + element.port + " ";
  158.                 //hostInfo = hostInfo + "]";
  159.                 //GUILayout.Label(hostInfo);
  160.                 //GUILayout.Space(5);
  161.                 //GUILayout.Label(element.comment);
  162.                 //GUILayout.Space(5);
  163.                 //GUILayout.FlexibleSpace();
  164.                 if (GUILayout.Button("Connect"))
  165.                 {
  166.                     // Connect to HostData struct, internally the correct method is used (GUID when using NAT).
  167.                     Network.Connect(element);
  168.                     Connected = true;
  169.                     //elementtoconnect = element;
  170.                     //PasswordEntry=true;
  171.                     //PasswordEntryMode = true;
  172.                 }
  173.                 GUILayout.EndHorizontal();
  174.             }
  175.  
  176.             GUILayout.EndVertical ();
  177.  
  178.             GUILayout.EndScrollView ();
  179.  
  180.             GUI.DragWindow ();
  181.         }
  182.  
  183.         void DoMyWindow2 (int windowID) {
  184.             scrollPos = GUILayout.BeginScrollView (scrollPos,false,false);
  185.             //GUI.TextArea (new Rect (10, 10, 230, 240),ChatText);
  186.             GUILayout.TextArea (ChatText);
  187.             GUILayout.EndScrollView ();
  188.  
  189.             GUILayout.BeginHorizontal ();
  190.  
  191.             if (Event.current.Equals( Event.KeyboardEvent ("return"))) {
  192.                 if (Connected) {
  193.                     networkView.RPC ("PostChat", RPCMode.All, Username+": "+ChatTextEntry);
  194.                     ChatTextEntry = "";
  195.                 }
  196.             }
  197.  
  198.             ChatTextEntry = GUILayout.TextField (ChatTextEntry);
  199.  
  200.             if (GUILayout.Button ("POST")) {
  201.                 if (Connected) {
  202.                     networkView.RPC ("PostChat", RPCMode.All, Username+": "+ChatTextEntry);
  203.                     ChatTextEntry = "";
  204.                 }
  205.                 //PostChat(ChatTextEntry);
  206.             }
  207.             GUILayout.EndHorizontal ();
  208.  
  209.             GUILayout.BeginHorizontal ();
  210.             GUILayout.Label ("Username: ");
  211.             Username = GUILayout.TextField (Username);
  212.             GUILayout.EndHorizontal ();
  213.  
  214.             GUI.DragWindow ();
  215.         }
  216.  
  217.         void DoMyWindow3 (int windowID) {
  218.  
  219.             GUILayout.Label ("Leave blank if there is no password.\n");
  220.  
  221.             GUILayout.BeginHorizontal ();
  222.  
  223.             GUILayout.Label ("Password: ");
  224.  
  225.             Password = GUILayout.TextField (Password);
  226.  
  227.             GUILayout.EndHorizontal ();
  228.  
  229.             GUILayout.Label ("\n");
  230.  
  231.             if (GUILayout.Button ("Done")) {
  232.                 PasswordEntry = false;
  233.                 if (!PasswordEntryMode) {
  234.                     Network.incomingPassword = Password;
  235.                     Network.InitializeServer (1, 25002, !Network.HavePublicAddress ());
  236.                     MasterServer.RegisterHost ("Besiege Server", ServerName);
  237.                     Connected = true;
  238.                 } else {
  239.                     try {
  240.                         Network.Connect(elementtoconnect,Password);
  241.                         Connected = true;
  242.                     } catch {
  243.                         Debug.Log ("An error occured on connection, most likely you didn't enter the correct password.");
  244.                     }
  245.                 }
  246.             }
  247.  
  248.             GUI.DragWindow ();
  249.         }
  250.  
  251.         [RPC]
  252.         void PostChat(string Text) {
  253.             ChatText += Text + "\n";
  254.         }
  255.  
  256.         [RPC]
  257.         void MassSuicide() {
  258.             foreach (NetworkBlock networkBlock in FindObjectsOfType<NetworkBlock>() as NetworkBlock[]) {
  259.                 Destroy(networkBlock.gameObject);
  260.             }
  261.         }
  262.  
  263.         [RPC]
  264.         void NetInstant(Vector3 g,Quaternion r,int id, int bid) {
  265.             GameObject temp;
  266.  
  267.             if (id == -1) {
  268.                 temp = (GameObject)Instantiate (GameObject.Find("bgeL0").gameObject, g, r);
  269.             } else {
  270.                 temp = (GameObject)Instantiate (FindObjectOfType<AddPiece> ().blockTypes [id].gameObject, g, r);
  271.             }
  272.  
  273.  
  274.  
  275.             try {
  276.                 Destroy (temp.transform.FindChild ("KeyMapVis").gameObject);
  277.             } catch {
  278.  
  279.             }
  280.  
  281.             try {
  282.                 Destroy (temp.transform.FindChild ("DirectionArrow").gameObject);
  283.             } catch {
  284.  
  285.             }
  286.             Destroy(temp.GetComponent<MyBlockInfo> ());
  287.             temp.AddComponent<NetworkBlock> ();
  288.             temp.GetComponent<NetworkBlock> ().id = bid;
  289.             networkBlocks.Add (temp);
  290.         }
  291.  
  292.         [RPC]
  293.         void NetUpdate(byte[] x, byte[] y, byte[] z, byte[] qx, byte[] qy, byte[] qz, byte[] qw, byte[] id) {
  294.             BinaryFormatter tempBinaryFormatter = new BinaryFormatter ();
  295.  
  296.             MemoryStream tempXStream = new MemoryStream (x);
  297.             List<float> tempXArray = (List<float>) tempBinaryFormatter.Deserialize (tempXStream);
  298.  
  299.             MemoryStream tempYStream = new MemoryStream (y);
  300.             List<float> tempYArray = (List<float>) tempBinaryFormatter.Deserialize (tempYStream);
  301.  
  302.             MemoryStream tempZStream = new MemoryStream (z);
  303.             List<float> tempZArray = (List<float>) tempBinaryFormatter.Deserialize (tempZStream);
  304.  
  305.             //
  306.  
  307.             MemoryStream tempQXStream = new MemoryStream (qx);
  308.             List<float> tempQXArray = (List<float>) tempBinaryFormatter.Deserialize (tempQXStream);
  309.  
  310.             MemoryStream tempQYStream = new MemoryStream (qy);
  311.             List<float> tempQYArray = (List<float>) tempBinaryFormatter.Deserialize (tempQYStream);
  312.  
  313.             MemoryStream tempQZStream = new MemoryStream (qz);
  314.             List<float> tempQZArray = (List<float>) tempBinaryFormatter.Deserialize (tempQZStream);
  315.  
  316.             MemoryStream tempQWStream = new MemoryStream (qw);
  317.             List<float> tempQWArray = (List<float>) tempBinaryFormatter.Deserialize (tempQWStream);
  318.  
  319.             //
  320.  
  321.             MemoryStream tempIntStream = new MemoryStream (id);
  322.             List<int> tempIntArray = (List<int>) tempBinaryFormatter.Deserialize (tempIntStream);
  323.  
  324.             //Debug.Log ("TEST");
  325.  
  326.             foreach (NetworkBlock b in FindObjectsOfType<NetworkBlock>() as NetworkBlock[]) {
  327.                 for (int i = 0;i<tempIntArray.Count;i++) {
  328.                     if (b.id == tempIntArray[i]) {
  329.                         //Debug.Log ("TEST2");
  330.                         b.transform.position = new Vector3 (tempXArray [i], tempYArray [i], tempZArray [i]);
  331.                         b.transform.rotation = new Quaternion (tempQXArray [i], tempQYArray [i], tempQZArray [i], tempQWArray [i]);
  332.                     }
  333.                 }
  334.             }
  335.         }
  336.  
  337.         void NetUpdateLoop() {
  338.             networkView.RPC ("MassSuicide", RPCMode.Others);
  339.             int bi = 0;
  340.             foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
  341.                 int i = 0;
  342.                 foreach (Transform Block2 in FindObjectOfType<AddPiece>().blockTypes) {
  343.                     try {
  344.                         if (Block2.GetComponent<MyBlockInfo> ().blockName == Block.blockName) {
  345.                             networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, i, bi);
  346.                         }
  347.                     } catch {
  348.  
  349.                     }
  350.                     i++;
  351.                 }
  352.                 if (Block.gameObject.name == "bgeL0" || Block.gameObject.name == "StartingBlock") {
  353.                     networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, -1, bi);
  354.                 }
  355.                 //networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position, Block.gameObject.transform.rotation, blockTypeValues[Block.blockName], bi);
  356.                 //}
  357.                 bi++;
  358.                 //yield return new WaitForSeconds (0.001f);
  359.                 //yield return new WaitForSeconds (2f);
  360.             }
  361.  
  362.         }
  363.  
  364.         IEnumerator<WaitForSeconds> Loop2() {
  365.             if (Connected) {
  366.                 if (GameObject.Find ("bgeL0") || GameObject.Find ("StartingBlock")) {
  367.                     //Debug.Log ("TEST");
  368.  
  369.                     //VECTOR 3
  370.  
  371.                     List<float> tempXArray = new List<float> ();
  372.                     MemoryStream tempXStream = new MemoryStream ();
  373.  
  374.                     List<float> tempYArray = new List<float> ();
  375.                     MemoryStream tempYStream = new MemoryStream ();
  376.  
  377.                     List<float> tempZArray = new List<float> ();
  378.                     MemoryStream tempZStream = new MemoryStream ();
  379.  
  380.                     // ---
  381.  
  382.                     //QUATERNION
  383.  
  384.                     List<float> tempQXArray = new List<float> ();
  385.                     MemoryStream tempQXStream = new MemoryStream ();
  386.  
  387.                     List<float> tempQYArray = new List<float> ();
  388.                     MemoryStream tempQYStream = new MemoryStream ();
  389.  
  390.                     List<float> tempQZArray = new List<float> ();
  391.                     MemoryStream tempQZStream = new MemoryStream ();
  392.  
  393.                     List<float> tempQWArray = new List<float> ();
  394.                     MemoryStream tempQWStream = new MemoryStream ();
  395.  
  396.                     // ---
  397.  
  398.                     List<int> tempIntArray = new List<int> ();
  399.                     MemoryStream tempIntStream = new MemoryStream ();
  400.  
  401.                     BinaryFormatter tempBinaryFormatter = new BinaryFormatter ();
  402.  
  403.                     int bi = 0;
  404.                     foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
  405.                         //networkView.RPC ("NetUpdate", RPCMode.Others, Block.transform.position, Block.transform.rotation, bi);
  406.                         //tempVector3Array.Add(Block.transform.position);
  407.                         tempXArray.Add(Block.transform.position.x);
  408.                         tempYArray.Add(Block.transform.position.y);
  409.                         tempZArray.Add(Block.transform.position.z);
  410.  
  411.                         tempQXArray.Add(Block.transform.rotation.x);
  412.                         tempQYArray.Add(Block.transform.rotation.y);
  413.                         tempQZArray.Add(Block.transform.rotation.z);
  414.                         tempQWArray.Add(Block.transform.rotation.w);
  415.  
  416.                         //tempQuaternionArray.Add (Block.transform.rotation);
  417.                         tempIntArray.Add (bi);
  418.                         bi++;
  419.                     }
  420.                     tempBinaryFormatter.Serialize (tempXStream, tempXArray);
  421.                     tempBinaryFormatter.Serialize (tempYStream, tempYArray);
  422.                     tempBinaryFormatter.Serialize (tempZStream, tempZArray);
  423.  
  424.                     tempBinaryFormatter.Serialize (tempQXStream, tempQXArray);
  425.                     tempBinaryFormatter.Serialize (tempQYStream, tempQYArray);
  426.                     tempBinaryFormatter.Serialize (tempQZStream, tempQZArray);
  427.                     tempBinaryFormatter.Serialize (tempQWStream, tempQWArray);
  428.  
  429.                     tempBinaryFormatter.Serialize (tempIntStream, tempIntArray);
  430.  
  431.                     networkView.RPC ("NetUpdate", RPCMode.Others,
  432.                         tempXStream.GetBuffer(),
  433.                         tempYStream.GetBuffer(),
  434.                         tempZStream.GetBuffer(),
  435.                         tempQXStream.GetBuffer(),
  436.                         tempQYStream.GetBuffer(),
  437.                         tempQZStream.GetBuffer(),
  438.                         tempQWStream.GetBuffer(),
  439.                         tempIntStream.GetBuffer());
  440.                 }
  441.             }
  442.             /**if (FindObjectOfType<MachineObjectTracker> () || FindObjectOfType<MachineObjectTracker> ().allBlockPositions.Count != 0) {
  443.                 yield return new WaitForSeconds (FindObjectOfType<MachineObjectTracker> ().allBlockPositions.Count / 100);
  444.             } else {
  445.                 yield return new WaitForSeconds (0.01f);
  446.             }**/
  447.             yield return new WaitForSeconds (0.06f);
  448.             StartCoroutine (Loop2());
  449.         }
  450.  
  451.  
  452.         void Update() {
  453.             if (!AddPiece.isSimulating) {
  454.                 UpdateInstant = true;
  455.             }
  456.  
  457.             if (Connected) {
  458.                 if (GameObject.Find ("bgeL0") || GameObject.Find ("StartingBlock")) {
  459.                     if (AddPiece.isSimulating && UpdateInstant == true) {
  460.                         //setUp ();
  461.                         NetUpdateLoop ();
  462.                         UpdateInstant = false;
  463.                     }
  464.                 }
  465.             }
  466.  
  467.             /**if (Input.GetKeyDown ("d")) {
  468.                
  469.  
  470.                 //StartCoroutine ("Loop");
  471.  
  472.                 //networkView.RPC ("MassSuicide", RPCMode.Others);
  473.                 foreach (MyBlockInfo Block in FindObjectsOfType<MyBlockInfo>() as MyBlockInfo[]) {
  474.                     /**foreach (Transform Block2 in FindObjectOfType<AddPiece>().blockTypes) {
  475.                             try {
  476.                                 if (Block2.GetComponent<MyBlockInfo>().blockName==Block.blockName&&Block.name!="CopyPasteInfo") {
  477.                                     //networkView.RPC("Suicide",RPCMode.AllBuffered);
  478.                                     Vector3 tempPos = Block.transform.position;
  479.                                     Quaternion tempRot = Block.transform.rotation;
  480.                                     Destroy(Block.gameObject);
  481.                                     GameObject temp = (GameObject) Network.Instantiate (Block2, tempPos, tempRot, 0);
  482.                                 }
  483.                             } catch {
  484.  
  485.                             }
  486.                         }
  487.                         //Network.Instantiate (Block, Block.transform.position, Block.transform.rotation, 0);
  488.                         //Destroy(Block.gameObject);
  489.  
  490.                         if (!Block.GetComponent<NetworkBlock> ()) {
  491.                             Block.gameObject.AddComponent<NetworkBlock> ();
  492.                             Block.gameObject.AddComponent<NetworkView> ();
  493.                         }**/
  494.                    
  495.                     //networkView.RPC ("NetInstant", RPCMode.Others, Block.gameObject.transform.position);
  496.                     //}
  497.  
  498.                     //networkView.RPC ("MassSuicide", RPCMode.Others);
  499.  
  500.                     //Network.Instantiate (FindObjectOfType<AddPiece>().blockTypes[0], new Vector3 (0, 0, 0), new Quaternion (), 0);
  501.                     /**Debug.Log(Block.blockName+":");
  502.                     for (int i = 0; i < Block.transform.childCount; i++) {
  503.                         Debug.Log (Block.transform.GetChild (i).name);
  504.                     }
  505.                 }
  506.             }**/
  507.         }
  508.     }
  509.  
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement