Jump to content

gudenau

Members
  • Posts

    5
  • Joined

  • Last visited

Reputation

0 Neutral

Profile Information

  • About me
    Bottle Rocketeer
  1. DontDestroyOnLoad(transform.gameObject);
  2. Thanks, where did you find that? Edit: DontDestroyOnLoad does not seem to exist... I feel like a I made a great big derp.
  3. The boolean is a one-shot, if true it will be run every time, not kept running. I see no KSPAddonFixed.
  4. I am attempting to create a Socket in a second thread to communicate with a different application. The application will have the ships stats displayed for now, but more will come later. My problem is that my Thread/Socket seems to die as soon as the player leaves the Main Menu, any help would be appreciated. Server (Java): package org.gudenau.pc.kerbal.kerbaldisplay; import java.awt.GridLayout; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class KerbalDisplay { private final static HashMap<Integer, Class<? extends Packet>> packetMap = new HashMap<Integer, Class<? extends Packet>>(); private JFrame frame; private JLabel active; private void main() throws IOException { ServerSocket server = new ServerSocket(1001); while(true){ Socket client = server.accept(); frame = new JFrame("Kerbal Display"); JPanel panel = new JPanel(new GridLayout(2, 0)); frame.add(panel); panel.add(new JLabel("Vessel != null:")); active = new JLabel("false"); panel.add(active); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); byte[] buffer = null; byte tmp = -1; int bufferOffset = -1; int type = -1; int count = 0; try { Thread.sleep(10); } catch (InterruptedException e1) {} while(true){ out.write(new byte[]{0, 0}); if(in.available() > 0){ tmp = (byte) in.read(); if(buffer == null){ buffer = new byte[tmp]; }else if(type == -1){ type = tmp; bufferOffset = 0; }else{ buffer[bufferOffset++] = tmp; if(bufferOffset == buffer.length){ processPacket(type, buffer); buffer = null; type = -1; } } }else{ try { Thread.sleep(1); } catch (InterruptedException e) {} } count++; if(count >= 1024){ count = 0; byte[] outBuffer = new byte[]{1, 0, 0}; System.out.println(outBuffer[0]); out.write(outBuffer); } } } } private void processPacket(int type, byte[] buffer) { switch(type){ case 0: active.setText((buffer[0] == 1) + ""); } } public static void main(String[] args) throws IOException{ new KerbalDisplay().main(); } } Plugin: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Net; using System.Net.Sockets; using System.IO; using UnityEngine; public class HelloKerbinModController { public void start() { IPAddress address = IPAddress.Loopback; IPEndPoint ipEndPoint = new IPEndPoint(address, 1001); socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.NoDelay = false; socket.Connect(ipEndPoint); byte[] tmp = null; byte data = 0; int rec; int tmpOffset = 0; while(socket.IsBound){ if(socket.Available > 0){ tmp = new byte[socket.Available]; rec = socket.Receive(tmp); for (tmpOffset = 0; tmpOffset < rec; tmpOffset++) { data = tmp[tmpOffset]; handleInputByte(data); } } } } private byte[] buffer = null; private int bufferOffset = 0; private int type = -1; private Socket socket; private void handleInputByte(byte data){ if(buffer == null){ buffer = new byte[data]; bufferOffset = 0; } else if (type == -1) { type = data; } else { buffer[bufferOffset++] = data; if(bufferOffset == buffer.Length){ processPacket(); buffer = null; type = -1; } } } private void processPacket() { switch(type){ case 0: sendVesselData(); break; } } private void sendVesselData() { Vessel ship = FlightGlobals.ActiveVessel; byte[] data = {1, 0, (byte)(ship == null ? 1 : 0) }; socket.Send(data); } private void informUser(String message) { ScreenMessages.PostScreenMessage(message, 30F, ScreenMessageStyle.UPPER_RIGHT); MonoBehaviour.print(message); } } [KSPAddon(KSPAddon.Startup.MainMenu, false)] public class HelloKerbinModConnection : MonoBehaviour { public void Start() { HelloKerbinModController hkmc = new HelloKerbinModController(); Thread thread = new Thread(hkmc.start); thread.Start(); } }
×
×
  • Create New...