public Dialog saveDialog(int categoryId, Dialog dialog){
		DialogCategory category = categories.get(categoryId);
		if(category == null)
			return dialog;
		dialog.category = category;

		while(containsDialogName(dialog.category, dialog)){
			dialog.setTitle(dialog.getTitle() + "_");
		}
		if(dialog.id < 0){
			lastUsedDialogID++;
			dialog.id = lastUsedDialogID;
		}
		
    	dialogs.put(dialog.id, dialog);
    	category.dialogs.put(dialog.id, dialog);
    	
    	File dir = new File(this.dir, category.getTitle());
    	if(!dir.exists())
    		dir.mkdirs();

    	File file = new File(dir, dialog.id + ".json_new");
    	File file2 = new File(dir, dialog.id + ".json");
    	
    	try {
    		dialog.data.save(file);
			if(file2.exists())
				file2.delete();
			file.renameTo(file2);
		} catch (Exception e) {
			LogWriter.except(e);
		}
		return dialog;
	}