فهرست منبع

Tweaks to networking classes, made UI text control size better, basic remote debugger for the IDE/Player

Ivan Safrin 13 سال پیش
والد
کامیت
0abcb9eefd

+ 3 - 1
Core/Contents/Include/PolyCocoaCore.h

@@ -151,7 +151,9 @@ namespace Polycode {
 				
 	protected:	
 		PolycodeView *glView;
-		uint64_t initTime;		
+		uint64_t initTime;	
+		
+		PolycodeView *view;
 		
 		IOHIDManagerRef hidManager;
 	};

+ 1 - 1
Core/Contents/Include/PolyServer.h

@@ -69,7 +69,7 @@ namespace Polycode {
 
 	class _PolyExport Server : public Peer {
 		public:
-			Server(unsigned int port, unsigned int rate, ServerWorld *world);
+			Server(unsigned int port, unsigned int rate, ServerWorld *world = NULL);
 			~Server();
 		
 			void DisconnectClient(ServerClient *client);

+ 1 - 1
Core/Contents/Include/PolySocket.h

@@ -30,7 +30,7 @@ THE SOFTWARE.
 #define MAX_PACKET_SIZE 400
 
 // if set to 1, will create a thread for each network socket
-#define USE_THREADED_SOCKETS 1
+#define USE_THREADED_SOCKETS 0
 
 // Socket poll interval time in msecs
 #define SOCKET_POLL_INTERVAL 5

+ 4 - 3
Core/Contents/Source/PolyClient.cpp

@@ -63,13 +63,14 @@ void Client::handlePacket(Packet *packet, PeerConnection *connection) {
 			case PACKET_TYPE_SETCLIENT_ID: {
 				clientID = (unsigned short)*packet->data;
 				ClientEvent *newEvent = new ClientEvent();
-				dispatchEvent(newEvent, ClientEvent::EVENT_CLIENT_READY);
-				sendReliableData(serverAddress, (char*)&clientID, sizeof(unsigned short), PACKET_TYPE_CLIENT_READY);
+				sendReliableData(serverAddress, (char*)&clientID, sizeof(unsigned short), PACKET_TYPE_CLIENT_READY);				
+				dispatchEvent(newEvent, ClientEvent::EVENT_CLIENT_READY);				
 			} break;
 			case PACKET_TYPE_DISONNECT:
 			{
 				ClientEvent *newEvent = new ClientEvent();
 				dispatchEvent(newEvent, ClientEvent::EVENT_SERVER_DISCONNECTED);
+				connected = false;
 			}
 			break;
 			default: {
@@ -91,7 +92,7 @@ void Client::setPersistentData(void *data, unsigned int size) {
 
 void Client::Connect(std::string ipAddress, unsigned int port) {
 	serverAddress.setAddress(ipAddress, port);
-	connected = true;	
+	connected = true;		
 }
 
 void Client::Disconnect() {

+ 3 - 0
Core/Contents/Source/PolyCocoaCore.mm

@@ -48,6 +48,8 @@ CocoaCore::CocoaCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen,
 	hidManager = NULL;
 	initGamepad();
 
+	this->view = view;
+
 	eventMutex = createMutex();
 	
 //	NSLog(@"BUNDLE: %@", [[NSBundle mainBundle] bundlePath]);
@@ -292,6 +294,7 @@ vector<Polycode::Rectangle> CocoaCore::getVideoModes() {
 
 CocoaCore::~CocoaCore() {
 	printf("Shutting down cocoa core\n");
+	[view setCore:nil];	
 	shutdownGamepad();
 	if(fullScreen) {
 		[glView exitFullScreenModeWithOptions:nil];

+ 2 - 0
Core/Contents/Source/PolyPeer.cpp

@@ -103,6 +103,8 @@ Packet *Peer::createPacket(const Address &target, char *data, unsigned int size,
 
 void Peer::sendReliableData(const Address &target, char *data, unsigned int size, unsigned short type) {	
 	PeerConnection *connection = getPeerConnection(target);	
+	if(!connection)
+		connection = addPeerConnection(target);	
 	Packet *packet = createPacket(target, data, size, type);
 	packet->header.reliableID = connection->reliableID;
 	connection->reliableID++;

+ 10 - 8
Core/Contents/Source/PolyServer.cpp

@@ -39,7 +39,7 @@ void ServerClient::handlePacket(Packet *packet) {
 	ServerClientEvent *event = new ServerClientEvent();	
 	event->data = packet->data;
 	event->dataSize = packet->header.size;
-	event->dataType = packet->header.type;	
+	event->dataType = packet->header.type;		
 	dispatchEvent(event, ServerClientEvent::EVENT_CLIENT_DATA);	
 }
 
@@ -67,13 +67,15 @@ void Server::handleEvent(Event *event) {
 	
 	ServerClient *client;		
 	if(event->getDispatcher() == rateTimer) {
-		world->updateWorld(rateTimer->getElapsedf());		
-		for(int i=0; i < clients.size(); i++) {
-			client = clients[i];
-			unsigned int worldDataSize;
-			char *worldData;
-			world->getWorldState(client, &worldData, &worldDataSize);			
-			sendData(client->connection->address, (char*)worldData, worldDataSize, PACKET_TYPE_SERVER_DATA);			
+		if(world) {
+			world->updateWorld(rateTimer->getElapsedf());		
+			for(int i=0; i < clients.size(); i++) {
+				client = clients[i];
+				unsigned int worldDataSize;
+				char *worldData;
+				world->getWorldState(client, &worldData, &worldDataSize);			
+				sendData(client->connection->address, (char*)worldData, worldDataSize, PACKET_TYPE_SERVER_DATA);			
+			}
 		}
 	}	
 	

+ 6 - 0
IDE/Build/Mac OS X/Polycode.xcodeproj/project.pbxproj

@@ -32,6 +32,7 @@
 		6DCE857B12AE018800566FAE /* PolycodeEditorManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DCE857912AE018800566FAE /* PolycodeEditorManager.cpp */; };
 		6DCE85B712AE082B00566FAE /* PolycodeImageEditor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DCE85B612AE082B00566FAE /* PolycodeImageEditor.cpp */; };
 		6DCE865512AE146C00566FAE /* PolycodeFontEditor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DCE865412AE146C00566FAE /* PolycodeFontEditor.cpp */; };
+		6DD1835D1628A4CC005A5682 /* PolycodeRemoteDebugger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DD1835C1628A4CC005A5682 /* PolycodeRemoteDebugger.cpp */; };
 		6DFBF64A12A3758400C43A7D /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6DFBF64812A3758400C43A7D /* OpenAL.framework */; };
 		6DFBF64B12A3758400C43A7D /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6DFBF64912A3758400C43A7D /* OpenGL.framework */; };
 		6DFE89BD14B3FC0900DA7ABD /* libfreetype.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6DFE89B614B3FC0900DA7ABD /* libfreetype.a */; };
@@ -113,6 +114,8 @@
 		6DCE85B612AE082B00566FAE /* PolycodeImageEditor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PolycodeImageEditor.cpp; sourceTree = "<group>"; };
 		6DCE865312AE146600566FAE /* PolycodeFontEditor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PolycodeFontEditor.h; sourceTree = "<group>"; };
 		6DCE865412AE146C00566FAE /* PolycodeFontEditor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PolycodeFontEditor.cpp; sourceTree = "<group>"; };
+		6DD1835C1628A4CC005A5682 /* PolycodeRemoteDebugger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PolycodeRemoteDebugger.cpp; sourceTree = "<group>"; };
+		6DD1835F1628A4D2005A5682 /* PolycodeRemoteDebugger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PolycodeRemoteDebugger.h; sourceTree = "<group>"; };
 		6DFBF64812A3758400C43A7D /* OpenAL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenAL.framework; path = System/Library/Frameworks/OpenAL.framework; sourceTree = SDKROOT; };
 		6DFBF64912A3758400C43A7D /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = /System/Library/Frameworks/OpenGL.framework; sourceTree = "<absolute>"; };
 		6DFE89B614B3FC0900DA7ABD /* libfreetype.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libfreetype.a; path = ../../../Release/Darwin/Framework/Core/Dependencies/lib/libfreetype.a; sourceTree = "<group>"; };
@@ -247,6 +250,7 @@
 		6D80E91212AB53FB0037A708 /* Include */ = {
 			isa = PBXGroup;
 			children = (
+				6DD1835F1628A4D2005A5682 /* PolycodeRemoteDebugger.h */,
 				6D3DC79F16220440003ED2C9 /* PolycodeConsole.h */,
 				6D56156514C542FB00FC8BD4 /* PolycodeScreenEditor.h */,
 				6D2AC99D14B8500A00BB63DA /* PolycodeProjectEditor.h */,
@@ -274,6 +278,7 @@
 		6D80E91812AB53FB0037A708 /* Source */ = {
 			isa = PBXGroup;
 			children = (
+				6DD1835C1628A4CC005A5682 /* PolycodeRemoteDebugger.cpp */,
 				6D3DC79C1622043A003ED2C9 /* PolycodeConsole.cpp */,
 				6D56156714C5430300FC8BD4 /* PolycodeScreenEditor.cpp */,
 				6D2AC99A14B8500400BB63DA /* PolycodeProjectEditor.cpp */,
@@ -400,6 +405,7 @@
 				6D2AC99B14B8500400BB63DA /* PolycodeProjectEditor.cpp in Sources */,
 				6D56156814C5430300FC8BD4 /* PolycodeScreenEditor.cpp in Sources */,
 				6D3DC79D1622043A003ED2C9 /* PolycodeConsole.cpp in Sources */,
+				6DD1835D1628A4CC005A5682 /* PolycodeRemoteDebugger.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 6 - 0
IDE/Contents/Include/PolycodeConsole.h

@@ -27,6 +27,8 @@
 
 using namespace Polycode;
 
+class PolycodeRemoteDebugger;
+
 class PolycodeConsole : public UIElement {
 	public:
 		PolycodeConsole();
@@ -36,6 +38,8 @@ class PolycodeConsole : public UIElement {
 		
 		void handleEvent(Event *event);
 		
+		void setDebugger(PolycodeRemoteDebugger *debugger);
+		
 		static void print(String msg);
 		
 		void Resize(Number width, Number height);
@@ -43,6 +47,8 @@ class PolycodeConsole : public UIElement {
 		static void setInstance(PolycodeConsole *newInstance);
 		
 	protected:
+	
+		PolycodeRemoteDebugger *debugger;
 		
 		static PolycodeConsole *instance;
 		

+ 3 - 2
IDE/Contents/Include/PolycodeFrame.h

@@ -72,7 +72,9 @@ public:
 		
 	UIImageButton *playButton;
 	UIHSizer *mainSizer;	
-			
+	
+	PolycodeConsole *console;
+						
 private:
 	
 	int frameSizeX;
@@ -89,7 +91,6 @@ private:
 	
 	ScreenImage *welcomeImage;	
 	
-	PolycodeConsole *console;
 	
 	EditorHolder *editorHolder;
 	

+ 4 - 0
IDE/Contents/Include/PolycodeIDEApp.h

@@ -38,6 +38,8 @@
 
 #include "PolycodeToolLauncher.h"
 
+#include "PolycodeRemoteDebugger.h"
+
 using namespace Polycode;
 
 class PolycodeIDEApp : public EventDispatcher {
@@ -75,4 +77,6 @@ protected:
 	PolycodeEditorManager *editorManager;
 	PolycodeProjectManager *projectManager;
 	
+	PolycodeRemoteDebugger *debugger;
+	
 };

+ 57 - 0
IDE/Contents/Include/PolycodeRemoteDebugger.h

@@ -0,0 +1,57 @@
+/*
+ Copyright (C) 2012 by Ivan Safrin
+ 
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ 
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ 
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+ 
+#pragma once
+
+#include "Polycode.h"
+#include "PolycodeConsole.h"
+
+using namespace Polycode;
+
+class DebuggerClient { 
+	public:
+		ServerClient *client;
+};
+
+class PolycodeRemoteDebugger : EventHandler {
+	public:
+		PolycodeRemoteDebugger();
+		~PolycodeRemoteDebugger();
+		
+		void injectCode(String code);
+		
+		void handleEvent(Event *event);
+			
+			
+		static const int EVENT_DEBUG_ERROR = 32;
+		static const int EVENT_DEBUG_PRINT = 33;
+		static const int EVENT_DEBUG_RESIZE = 34;
+		static const int EVENT_DEBUG_REMOVE = 35;
+
+		static const int EVENT_INJECT_CODE = 36;
+			
+	protected:
+		
+		Server *server;
+		std::vector<DebuggerClient*> debuggerClients;
+
+};

+ 12 - 0
IDE/Contents/Source/PolycodeConsole.cpp

@@ -21,10 +21,14 @@
 */
 
 #include "PolycodeConsole.h"
+#include "PolycodeRemoteDebugger.h"
 
 PolycodeConsole* PolycodeConsole::instance = NULL;
 
+
 PolycodeConsole::PolycodeConsole() : UIElement() {
+
+	debugger = NULL;
 	debugTextInput = new UITextInput(true, 100, 100);
 	addChild(debugTextInput);
 
@@ -42,11 +46,19 @@ PolycodeConsole::~PolycodeConsole() {
 
 }
 
+void PolycodeConsole::setDebugger(PolycodeRemoteDebugger *debugger) {
+	this->debugger = debugger;
+}
+
 void PolycodeConsole::handleEvent(Event *event) {
 	if(event->getDispatcher() == consoleTextInput) {
 		if(event->getEventCode() == Event::COMPLETE_EVENT) {
 			_print(">"+consoleTextInput->getText());
 			_print("\n");
+			if(debugger) {
+				debugger->injectCode(consoleTextInput->getText());
+			}	
+
 			consoleTextInput->setText("");
 		}
 	}

+ 2 - 0
IDE/Contents/Source/PolycodeIDEApp.cpp

@@ -75,6 +75,8 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() {
 	frame->Resize(core->getXRes(), core->getYRes());	
 	core->setVideoMode(1000, 600, false, false, 0, 0);
 	
+	debugger = new PolycodeRemoteDebugger();
+	frame->console->setDebugger(debugger);
 	
 //	CoreServices::getInstance()->getResourceManager()->addArchive(RESOURCE_PATH"tomato.polyapp");
 	

+ 102 - 0
IDE/Contents/Source/PolycodeRemoteDebugger.cpp

@@ -0,0 +1,102 @@
+/*
+ Copyright (C) 2012 by Ivan Safrin
+ 
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ 
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ 
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+#include "PolycodeRemoteDebugger.h"
+
+
+PolycodeRemoteDebugger::PolycodeRemoteDebugger() {
+	server = new Server(4630, 1);
+
+	server->addEventListener(this, ServerEvent::EVENT_CLIENT_CONNECTED);
+	server->addEventListener(this, ServerEvent::EVENT_CLIENT_DISCONNECTED);		
+
+}
+
+PolycodeRemoteDebugger::~PolycodeRemoteDebugger() {
+
+}
+
+void PolycodeRemoteDebugger::injectCode(String code) {
+	server->sendReliableDataToAllClients((char*)code.c_str(), code.length()+1, EVENT_INJECT_CODE);
+}
+
+void PolycodeRemoteDebugger::handleEvent(Event *event) {
+
+	for(int i=0; i < debuggerClients.size(); i++) {
+		if(event->getDispatcher() == debuggerClients[i]->client) {		
+			ServerClientEvent *clientEvent = (ServerClientEvent*) event;
+			DebuggerClient *client = debuggerClients[i];
+			switch(clientEvent->getEventCode()) {
+				case ServerClientEvent::EVENT_CLIENT_DATA:				
+					switch(clientEvent->dataType) {			
+						case EVENT_DEBUG_PRINT:
+						{
+							String printStr = String(clientEvent->data);
+							PolycodeConsole::print(printStr);		
+							PolycodeConsole::print("\n");
+						}
+						break;	
+						case EVENT_DEBUG_ERROR:
+						{
+							String printStr = String(clientEvent->data);
+							PolycodeConsole::print(printStr);		
+							PolycodeConsole::print("\n");							
+						}
+						break;							
+					}
+				break;
+			}
+		}
+	}
+
+
+	if(event->getDispatcher() == server) {
+		ServerEvent *serverEvent = (ServerEvent*) event;
+		switch(serverEvent->getEventCode()) {
+		
+			case ServerEvent::EVENT_CLIENT_DISCONNECTED:		
+			{
+				for(int i=0;i<debuggerClients.size();i++) {
+					if(debuggerClients[i]->client == serverEvent->client) {		
+						DebuggerClient *client = debuggerClients[i];
+						debuggerClients.erase(debuggerClients.begin()+i);	
+						delete client;
+						PolycodeConsole::print("Remote debugger client disconnected...\n");						
+					}
+				}
+			}	
+			break;
+			
+			case ServerEvent::EVENT_CLIENT_CONNECTED:
+			{
+				DebuggerClient *newClient = new DebuggerClient();
+				newClient->client = serverEvent->client;
+				newClient->client->addEventListener(this, ServerClientEvent::EVENT_CLIENT_DATA);
+				PolycodeConsole::print("Remote debugger client connected...\n");printf("CLIENT CONNECTED\n");		
+				debuggerClients.push_back(newClient);		
+			}
+			break;
+		}
+		
+	}
+
+}

+ 1 - 1
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -360,7 +360,7 @@ void UITextInput::restructLines() {
 	}
 	
 	if(scrollContainer) {
-		scrollContainer->setContentSize(width,  (((lines.size()) * ((lineHeight+lineSpacing)))) - padding);
+		scrollContainer->setContentSize(width,  (((lines.size()+1) * ((lineHeight+lineSpacing)))) - padding);
 	}	
 	
 }

+ 1 - 1
Player/Contents/Include/PolycodeCocoaPlayer.h

@@ -30,7 +30,7 @@ THE SOFTWARE.
 
 class CocoaPolycodePlayer : public PolycodePlayer {
 	public:
-		CocoaPolycodePlayer(PolycodeView *view,String fileName, bool knownArchive);
+		CocoaPolycodePlayer(PolycodeView *view,String fileName, bool knownArchive, bool useDebugger=false);
 		virtual ~CocoaPolycodePlayer();
 	
 		void createCore();	

+ 29 - 4
Player/Contents/Include/PolycodePlayer.h

@@ -35,12 +35,26 @@ extern "C" {
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
-// #include "lapi.h"
-	
+// #include "lapi.h"	
 
 using namespace Polycode;
 
-//class PolycodeRemoteDebuggerClient : public
+class PolycodeRemoteDebuggerClient : public EventDispatcher {
+	public:
+		PolycodeRemoteDebuggerClient();
+		~PolycodeRemoteDebuggerClient();
+		
+		void handleEvent(Event *event);
+		
+		static const int EVENT_DEBUG_ERROR = 32;
+		static const int EVENT_DEBUG_PRINT = 33;
+		static const int EVENT_DEBUG_RESIZE = 34;
+		static const int EVENT_DEBUG_REMOVE = 35;
+		
+		static const int EVENT_INJECT_CODE = 36;		
+	
+		Client *client;
+};
 
 class PolycodeDebugEvent : public Event {
 public:
@@ -63,7 +77,7 @@ class PolycodePlayer : public EventDispatcher {
 	
 public:
 	
-	PolycodePlayer(String fileName, bool knownArchive);
+	PolycodePlayer(String fileName, bool knownArchive, bool useDebugger=false);
 	virtual ~PolycodePlayer();
 	
 	void runPlayer();
@@ -93,9 +107,20 @@ public:
 	Core *core;	
 	
 protected:
+
+	Timer *debuggerTimer;
+	
+	PolycodeRemoteDebuggerClient *remoteDebuggerClient;
 	
 	lua_State *L;		
 	
+	bool useDebugger;
+	
+	String fullPath;
+	
+	bool doCodeInject;
+	String injectCodeString;
+	
 	std::vector<String> loadedModules;
 	
 	bool _knownArchive;

+ 1 - 1
Player/Contents/Platform/Darwin/MyDocument.mm

@@ -46,7 +46,7 @@ THE SOFTWARE.
 {
     [super windowControllerDidLoadNib:aController];
 	
-	player =  new CocoaPolycodePlayer(mainView, [docFileName cStringUsingEncoding:NSASCIIStringEncoding], false);
+	player =  new CocoaPolycodePlayer(mainView, [docFileName cStringUsingEncoding:NSASCIIStringEncoding], false, true);
 	playerProxy = new PolycodeProxy();
 	playerProxy->playerDocument = self;
 	player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_RESIZE);

+ 1 - 1
Player/Contents/Source/PolycodeCocoaPlayer.mm

@@ -22,7 +22,7 @@ THE SOFTWARE.
 #include "PolycodeCocoaPlayer.h"
 
 
-CocoaPolycodePlayer::CocoaPolycodePlayer(PolycodeView *view, String fileName, bool knownArchive) : PolycodePlayer(fileName, knownArchive) {
+CocoaPolycodePlayer::CocoaPolycodePlayer(PolycodeView *view, String fileName, bool knownArchive, bool useDebugger) : PolycodePlayer(fileName, knownArchive, useDebugger) {
 	this->view = view;
 }
 

+ 78 - 4
Player/Contents/Source/PolycodePlayer.cpp

@@ -23,6 +23,28 @@ THE SOFTWARE.
 #include "PolycodePlayer.h"
 #include <string>
 
+PolycodeRemoteDebuggerClient::PolycodeRemoteDebuggerClient() : EventDispatcher() {
+	client = new Client(6445, 1);
+	client->Connect("127.0.0.1", 4630);	
+}
+
+void PolycodeRemoteDebuggerClient::handleEvent(Event *event) {
+	PolycodeDebugEvent *debugEvent = (PolycodeDebugEvent*) event;
+	switch(event->getEventCode()) {
+		case PolycodeDebugEvent::EVENT_PRINT:
+			client->sendReliableDataToServer((char*)debugEvent->errorString.c_str(), debugEvent->errorString.length()+1, EVENT_DEBUG_PRINT);
+		break;
+		case PolycodeDebugEvent::EVENT_ERROR:
+			client->sendReliableDataToServer((char*)debugEvent->errorString.c_str(), debugEvent->errorString.length()+1, EVENT_DEBUG_ERROR);
+		break;		
+	}
+}
+
+PolycodeRemoteDebuggerClient::~PolycodeRemoteDebuggerClient() {
+	printf("disconnecting debugger\n");
+	client->Disconnect();
+}
+
 extern "C" {	
 //	extern int luaopen_Tau(lua_State* L); // declare the wrapped module
 		//	loadFileIntoState(L, "Polycode Player.app/Contents/Resources/API/class.lua");
@@ -290,9 +312,11 @@ PolycodeDebugEvent::~PolycodeDebugEvent() {
 }
 
 
-PolycodePlayer::PolycodePlayer(String fileName, bool knownArchive) : EventDispatcher()  {
+PolycodePlayer::PolycodePlayer(String fileName, bool knownArchive, bool useDebugger) : EventDispatcher()  {
 	L = NULL;
 
+	doCodeInject = false;
+	this->useDebugger = useDebugger;
 	fileToRun = fileName;
 	core = NULL;
 	doneLoading = false;	
@@ -301,6 +325,7 @@ PolycodePlayer::PolycodePlayer(String fileName, bool knownArchive) : EventDispat
 	yRes = 480;
 	aaLevel = 6;
 	fullScreen = false;	
+	
 }
 
 void PolycodePlayer::loadFile(const char *fileName) {
@@ -474,8 +499,6 @@ void PolycodePlayer::loadFile(const char *fileName) {
 //	CoreServices::getInstance()->getRenderer()->setClearColor(1,0,0);
 	srand(core->getTicks());
 	
-	String fullPath;
-	
 	if(loadingArchive) {
 		fullPath = mainFile;
 	} else {
@@ -485,7 +508,18 @@ void PolycodePlayer::loadFile(const char *fileName) {
 		Logger::log(fullPath.c_str());
 	}
 	
-	runFile(fullPath);
+	if(useDebugger) {
+		remoteDebuggerClient = new PolycodeRemoteDebuggerClient();
+		this->addEventListener(remoteDebuggerClient, PolycodeDebugEvent::EVENT_PRINT);
+		this->addEventListener(remoteDebuggerClient, PolycodeDebugEvent::EVENT_ERROR);		
+		remoteDebuggerClient->client->addEventListener(this, ClientEvent::EVENT_CLIENT_READY);
+		remoteDebuggerClient->client->addEventListener(this, ClientEvent::EVENT_SERVER_DATA);
+		
+		debuggerTimer = new Timer(true, 5000);
+		debuggerTimer->addEventListener(this, Timer::EVENT_TRIGGER);
+	} else{
+		runFile(fullPath);
+	}
 }
 
 void PolycodePlayer::runPlayer() {
@@ -494,6 +528,8 @@ void PolycodePlayer::runPlayer() {
 }
 
 PolycodePlayer::~PolycodePlayer() {
+	this->removeAllHandlers();
+	delete remoteDebuggerClient;
 	Logger::log("deleting core...\n");
 	delete core;
 	PolycodeDebugEvent *event = new PolycodeDebugEvent();			
@@ -502,6 +538,37 @@ PolycodePlayer::~PolycodePlayer() {
 }
 
 void PolycodePlayer::handleEvent(Event *event) {	
+
+	if(event->getDispatcher() == debuggerTimer) {
+		runFile(fullPath);
+		debuggerTimer->Pause(true);
+	}
+
+	if(event->getDispatcher() == remoteDebuggerClient->client) {
+		ClientEvent *clientEvent = (ClientEvent*) event;
+			
+		switch(event->getEventCode()) {
+			case ClientEvent::EVENT_CLIENT_READY:
+				debuggerTimer->Pause(true);			
+				runFile(fullPath);			
+			break;
+			
+			case ClientEvent::EVENT_SERVER_DATA:
+			{
+				switch(clientEvent->dataType) {
+					case PolycodeRemoteDebuggerClient::EVENT_INJECT_CODE:
+					{
+						char *code = (char*) clientEvent->data;
+						injectCodeString = String(code);
+						doCodeInject = true;
+					}
+					break;										
+				}
+			}
+			break;			
+		}
+	}
+	
 	if(event->getDispatcher() == core) {
 		switch(event->getEventCode()) {
 			case Core::EVENT_CORE_RESIZE:
@@ -517,6 +584,13 @@ void PolycodePlayer::handleEvent(Event *event) {
 
 bool PolycodePlayer::Update() {
 	if(L) {
+		
+		if(doCodeInject) {
+			printf("INJECTING CODE:[%s]\n", injectCodeString.c_str());
+			doCodeInject = false;			
+			report(L, luaL_loadstring(L, injectCodeString.c_str()) || lua_pcall(L, 0,0,0));		
+		}
+	
 		lua_getfield(L, LUA_GLOBALSINDEX, "Update");
 		lua_pushnumber(L, core->getElapsed());
 		lua_call(L, 1, 0);