Editor / File Writer

Monkey Archive Forums/Monkey Projects/Editor / File Writer

Raz(Posted 2011) [#1]
Hi,

I would like to code my level editor in Monkey, this means I would like the ability to write files to the local machine at a location of my choice (and not the platform specific save locations).

This would only be required for a windows based build (I am assuming GLFW as XNA does not have write functions).

I appreciate this is outside of the scope of Monkey, but is it possible to add a SaveFileToDisk(filename:String,content:String) function for the GLFW build specifically? Would anyone know how do to do this?

Thanks
-Chris


Raz(Posted 2011) [#2]
Damnit, can this be moved to the Monkey programming section?


MonkeyPig(Posted 2011) [#3]
This isn't very clean/tidy etc. as I cobbled it together in a bit of a hurry but here's the code I used in my Editor...

String mpp_Load(int fileTypes )
{
    printf("MPPLoad()\n");

    TCHAR   szFile[MAX_PATH] = TEXT("\0");

    OPENFILENAME   ofn;
    HANDLE hFile = INVALID_HANDLE_VALUE;

    memset( &(ofn), 0, sizeof(ofn));


    switch (fileTypes)
    {
    case 0:
    default:
        ofn.lpstrFilter = TEXT("MPP Map File (*.mpp)\0*.mpp\0"); 
        ofn.lpstrDefExt = TEXT("mpp");  // 
        break;
    case 1:
        ofn.lpstrFilter = TEXT("png Image File (*.png)\0*.png\0");
        ofn.lpstrDefExt = TEXT("png");
        break;
    case 2:
        ofn.lpstrFilter = TEXT("TRG/MP1 SNES Map File(s) (*.TRG)\0*.TRG\0*.MP?\0");
        ofn.lpstrDefExt = TEXT("TRG");
        break;
    }
    
    ofn.lStructSize   = sizeof(ofn);
    ofn.hwndOwner = 0;  //hDlg;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrTitle = TEXT("Load File...");
    ofn.Flags = OFN_HIDEREADONLY; 


    //get the filename the user wants to Load to
    if (GetOpenFileName(&ofn)) 
    {
        // Display what we were about to Load...
        printf("Loading[%s]\n", ofn.lpstrFile);
        String txt = ofn.lpstrFile;
        return txt;
    }
    else
    {
        printf("Cancelled FILE Load\n");
    }
    return NULL;
}

void mpp_SaveAs(String dataText, String fileName)
{
    const char* pData = dataText.ToCString<char>();
    const char* pFileName = fileName.ToCString<char>();

    printf("MPPSaveAs(%s, %s)\n", pData, pFileName);

    TCHAR   szFile[MAX_PATH] = TEXT("\0");

    strcpy(szFile, (LPSTR)pFileName);

    OPENFILENAME   ofn;
    HANDLE hFile = INVALID_HANDLE_VALUE;
    
    memset( &(ofn), 0, sizeof(ofn));
    ofn.lStructSize   = sizeof(ofn);
    ofn.hwndOwner = 0;  //hDlg;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = TEXT("MPP Map File (*.mpp)\0*.mpp\0");   
    ofn.lpstrTitle = TEXT("Save File As");
    ofn.Flags = OFN_HIDEREADONLY; 
    ofn.lpstrDefExt = TEXT("mpp");

    //get the filename the user wants to save to
    if (GetSaveFileName(&ofn)) 
    {
        // Display what we were about to save...
        printf("Opening[%s]\n", ofn.lpstrFile);

        FILE* fp = fopen(ofn.lpstrFile, "wb+");
        if (fp!=NULL)
        {
            fwrite(pData, strlen(pData), 1, fp);
            fputc(0, fp);
            fclose(fp);

            printf("Success\n");
        }
        else
        {
            printf("Failed\n");
        }
    }
    else
    {
        printf("Cancelled FILE SAVEAS\n");
    }
}

int mpp_OpenFile(String s, String mode)
{
	FILE* fp=NULL;
	fp=fopen(s.ToCString<char>(), mode.ToCString<char>());
	return (int)fp;
}

void mpp_CloseFile(int fp)
{
	if((FILE *)fp)fclose((FILE *)fp);
}

void mpp_WriteFileInt(int fp,int i)
{
	fwrite(&i,sizeof(i),1,(FILE *)fp);
}

int mpp_FileRead(void* ptr, size_t size, size_t count, int stream)
{
	return fread(ptr, size, count, (FILE*)stream);
}
String mpp_FGets(int fp)
{
	int i=0;
    char pString[2048];
	fgets(pString, 2048, (FILE *)fp);
    String txt = pString;
	return txt;
}



MonkeyPig(Posted 2011) [#4]
And the Monkey side of it...

#If TARGET="html5" Then

    Import "extensions.js"        ' tell the compiler to include code from extensions.js

#ElseIf TARGET="android" Then
   
    Import "extensions.java"        ' tell the compiler to include code from extensions.js

#Else
    
	Import "extensions.cpp"       ' tell the compiler to include code from extensions.cpp

#Endif

Extern
	#If LANG="cpp" Or LANG="js" Then
    
        ' My Load/Save FileSelector stuff for Tool Development
    
        Function MPPAlert:Int(errorText:String)="mpp_Alert"
        Function MPPLoad:String(fileType:Int)="mpp_Load"
        Function MPPSaveAs:Void(dataText:String, fileName:String)="mpp_SaveAs"
    
        Function MPPOpenFile:Int(s:String,mode:String)="mpp_OpenFile"
    	Function MPPCloseFile:Void(fp:Int)="mpp_CloseFile"
    	Function MPPWriteFileInt:Void(fp:Int,i:Int)="mpp_WriteFileInt"
    	Function MPPReadFileInt:Int(fp:Int)="mpp_ReadFileInt"
        Function MPPFGets:String(fp:Int)="mpp_FGets"
#Endif
Public




MonkeyPig(Posted 2011) [#5]
Usage:
Local io:String = "My Map Format as a text file"
MPPSaveAs(io, "Test.your_extension")


This opens a File SaveAs Dialog and you can navigate to the save location.


Raz(Posted 2011) [#6]
Thanks MonkeyPig :) I'll have a look this weekend.