Added initial version

This commit is contained in:
Ceikry
2021-03-07 20:37:32 -06:00
commit b452bd670c
13290 changed files with 1178433 additions and 0 deletions
@@ -0,0 +1,109 @@
package ms.classloader;
import ms.Management;
import ms.ServerConstants;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.HashMap;
/**
* ClassLoadServer
* @author Clayton Williams (Hope)
*/
public class ClassLoadServer extends Thread {
/**
* Listening port
*/
private static final int PORT = 5050;
/**
* serverSocket for incoming connections
*/
private ServerSocket serverSocket = null;
/**
* Holds classes and resources already added in the server
*/
protected static HashMap<String, byte[]> resourceCache = new HashMap<String, byte[]>();
/**
* New socket
* @throws UnknownHostException
* @throws IOException
*/
public ClassLoadServer() {
try {
serverSocket = new ServerSocket(PORT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Listen for incoming connections
*/
@Override
public void run() {
resetResourceCache();
// System.out.println("Listening on port " + PORT + "...");
while (Management.active) {
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
//System.out.println("New Connection from : " + clientSocket.getInetAddress());
WorkerThread wcs = new WorkerThread(clientSocket);
wcs.start();
} catch (Exception e) {}
}
}
/**
* Loads resources recursively from a starting root directory and adds the file bytes to our cache
* @param path
* @param pathRoot
*/
private static void loadResources(String path, String pathRoot) {
try {
path = ServerConstants.fixPath(null, path);
pathRoot = ServerConstants.fixPath(null, pathRoot);
File root = new File(path);
File[] list = root.listFiles();
if (list == null) return;
for (File f : list) {
if (f.isDirectory()) {
loadResources(f.getAbsolutePath(), pathRoot);
} else {
if (f.exists()) {
if (!f.getName().startsWith(".")) {
byte[] bytes = Files.readAllBytes(f.toPath());
String name = ServerConstants.fixPath(null, f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(pathRoot)));
//System.out.println("[ClassLoadServer] Loaded resource '" + f.getName() +
// "' Size: " + NumberFormat.getInstance().format(bytes.length) + " bytes");
resourceCache.put(name, bytes);
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Empties the class cache and loads resources
*/
public static void resetResourceCache() {
resourceCache.clear();
//loadResources("bin/org/keldagrim/launcher/", "org/arios/launcher/");
//loadResources("resources/", "org/keldagrim/launcher/");
//System.out.println("Loaded all resources!");
}
}
@@ -0,0 +1,136 @@
package ms.classloader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Map;
import ms.ServerConstants;
import ms.system.OperatingSystem;
/**
* Worker thread for launcher connections
* @author Clayton Williams (Hope)
*
*/
public class WorkerThread extends Thread {
/**
* Our launcher connection
*/
private Socket launcher = null;
/**
* Input stream from launcher
*/
private ObjectInputStream is = null;
/**
* Output stream to launcher
*/
private ObjectOutputStream os = null;
/**
* The user's operating system type
*/
private OperatingSystem operatingSystem = OperatingSystem.WINDOWS;
/**
* Creates a new worker thread to handle the launcher requests
* @param socket
* @param classesCache
* @throws IOException
*/
public WorkerThread(Socket launcher) throws IOException {
super();
this.launcher = launcher;
try {
os = new ObjectOutputStream(new BufferedOutputStream(this.launcher.getOutputStream()));
os.flush();
is = new ObjectInputStream(new BufferedInputStream(launcher.getInputStream()));
} catch (IOException ioe) {
this.launcher.close();
}
}
/**
* Input Stream handler
*/
@SuppressWarnings("unused") //saving resource type for later
public void run() {
byte opcode = -1;
try {
while (true) {
opcode = is.readByte();
switch(opcode) {
case 1: //requests revision
operatingSystem = is.readUTF().contains("UNIX") ? OperatingSystem.UNIX : OperatingSystem.WINDOWS;
os.reset();
os.writeInt(ClassLoadServer.resourceCache.size());//#customresources
for (Map.Entry<String, byte[]> hm : ClassLoadServer.resourceCache.entrySet()) {
os.writeUTF(ServerConstants.fixPath(operatingSystem, (String) hm.getKey()));
os.writeInt(hm.getValue().length);
}
os.writeByte(1); //number of custom resources
//followed by 2 strings
//System.err.println(operatingSystem.name());
os.writeUTF(ServerConstants.fixPath(operatingSystem, "ms/launcher/arios-gamepack-530.jar"));
os.writeUTF("BINARY");
os.flush();
break;
case 2: //resource request
String resourceName = ServerConstants.fixPath(null, is.readUTF());
String resourceType = is.readUTF();
if (ClassLoadServer.resourceCache.containsKey(resourceName)) {
sendResource(ClassLoadServer.resourceCache.get(resourceName));
//System.out.println("Sent Resource: " + resourceName);
} else {
sendOpcode(-1);
//System.out.println("Could not send resource '" + resourceName + "'");
}
break;
default:
System.out.println("Unhandled opcode=" + opcode);
break;
}
}
} catch(Exception e) {
//System.out.println("Client force disconnect.");
} finally {
try {
is.close();
os.close();
launcher.close();
} catch (Exception e) {}
}
}
/**
* send a byte array packet to the client
* @exception IOException file read error.
*/
protected void sendResource(byte [] bytes) throws IOException {
os.reset();
os.writeByte(2);
os.writeInt(bytes.length);
for (int i = 0; i < bytes.length; i++) {
os.writeByte(bytes[i]);
}
os.flush();
}
/**
* Send no data opcodes
* @param opcode
* @throws IOException
*/
protected void sendOpcode(int opcode) throws IOException {
os.reset();
os.writeByte(opcode);
os.flush();
}
}