added generic function to add new level or artwork set to tree
authorHolger Schemel <info@artsoft.org>
Sun, 13 Jan 2019 12:10:50 +0000 (13:10 +0100)
committerHolger Schemel <info@artsoft.org>
Sun, 13 Jan 2019 12:10:50 +0000 (13:10 +0100)
src/events.c
src/libgame/setup.c
src/libgame/setup.h
src/libgame/system.h

index ac9e2ab0f54c429b9b2f996443505305038ee41e..f33b31169202c335e2bd30b789359515abe54262 100644 (file)
@@ -1546,12 +1546,7 @@ static void HandleDropFileEventExt(char *filename)
     char *top_dir = ExtractZipFileIntoDirectory(filename, directory, tree_type);
 
     if (top_dir != NULL)
-    {
-      if (tree_type == TREE_TYPE_LEVEL_DIR)
-       AddUserLevelSetToLevelInfo(top_dir);
-      else
-       AddUserArtworkSetToArtworkInfo(top_dir, tree_type);
-    }
+      AddUserTreeSetToTreeInfo(top_dir, tree_type);
   }
 }
 
index 2eca885ac16b27fb5ced808c25871f15239dd250..7818b7322eaa7a79faf4648d5a9ad85eb891b7a7 100644 (file)
@@ -3944,6 +3944,77 @@ void AddUserArtworkSetToArtworkInfo(char *artwork_subdir_new, int type)
     Error(ERR_EXIT, "internal artwork set structure corrupted -- aborting");
 }
 
+static boolean AddUserTreeSetToTreeInfoExt(char *tree_subdir_new, int type)
+{
+  TreeInfo **tree_node_first, *tree_node_old, *tree_node_new;
+  char *tree_user_dir = TREE_USERDIR(type);
+
+  if (tree_user_dir == NULL)           // should not happen
+    return FALSE;
+
+  // get first node of level or artwork tree
+  tree_node_first = TREE_FIRST_NODE_PTR(type);
+
+  if (tree_node_first == NULL)         // should not happen
+    return FALSE;
+
+  if (type == TREE_TYPE_LEVEL_DIR)
+  {
+    // get level info tree node of personal user level set
+    tree_node_old = getTreeInfoFromIdentifier(*tree_node_first, getLoginName());
+  }
+  else
+  {
+    // get artwork info tree node of first artwork set
+    tree_node_old = *tree_node_first;
+  }
+
+  if (tree_node_old == NULL)           // should not happen
+    return FALSE;
+
+  int draw_deactivation_mask = GetDrawDeactivationMask();
+
+  // override draw deactivation mask (temporarily disable drawing)
+  SetDrawDeactivationMask(REDRAW_ALL);
+
+  if (type == TREE_TYPE_LEVEL_DIR)
+  {
+    // load new level set config and add it next to first user level set
+    LoadLevelInfoFromLevelConf(&tree_node_old->next, NULL,
+                              tree_user_dir, tree_subdir_new);
+  }
+  else
+  {
+    // load new artwork set config and add it next to first artwork set
+    LoadArtworkInfoFromArtworkConf(&tree_node_old->next, NULL,
+                                  tree_user_dir, tree_subdir_new, type);
+  }
+
+  // set draw deactivation mask to previous value
+  SetDrawDeactivationMask(draw_deactivation_mask);
+
+  // get tree info tree node of newly added tree set
+  tree_node_new = getTreeInfoFromIdentifier(*tree_node_first, tree_subdir_new);
+
+  if (tree_node_new == NULL)           // should not happen
+    return FALSE;
+
+  // correct top link and parent node link of newly created tree node
+  tree_node_new->node_top    = tree_node_old->node_top;
+  tree_node_new->node_parent = tree_node_old->node_parent;
+
+  // sort tree info tree to adjust position of newly added tree set
+  sortTreeInfo(tree_node_first);
+
+  return TRUE;
+}
+
+void AddUserTreeSetToTreeInfo(char *tree_subdir_new, int type)
+{
+  if (!AddUserTreeSetToTreeInfoExt(tree_subdir_new, type))
+    Error(ERR_EXIT, "internal tree set structure corrupted -- aborting");
+}
+
 char *getArtworkIdentifierForUserLevelSet(int type)
 {
   char *classic_artwork_set = getClassicArtworkSet(type);
index cbd0edf5ee9dd5dfad7790644840143c0dbf338a..7740c2cb628318750bec61959cc9d1e17b404638 100644 (file)
@@ -331,6 +331,7 @@ TreeInfo *getArtworkTreeInfoForUserLevelSet(int);
 boolean checkIfCustomArtworkExistsForCurrentLevelSet(void);
 void AddUserLevelSetToLevelInfo(char *);
 void AddUserArtworkSetToArtworkInfo(char *, int);
+void AddUserTreeSetToTreeInfo(char *, int);
 boolean UpdateUserLevelSet(char *, char *, char *, int);
 boolean CreateUserLevelSet(char *, char *, char *, int, boolean);
 
index aaf4604ec0c1a431a7e1303835135ec9adce029c..5249adfcb599df787c497ba9aa6ba153949eedfb 100644 (file)
                                 getUserMusicDir() :                    \
                                 NULL)
 
+#define TREE_FIRST_NODE_PTR(t) ((t) == TREE_TYPE_LEVEL_DIR ?           \
+                                &leveldir_first :                      \
+                                (t) == TREE_TYPE_GRAPHICS_DIR ?        \
+                                &artwork.gfx_first :                   \
+                                (t) == TREE_TYPE_SOUNDS_DIR ?          \
+                                &artwork.snd_first :                   \
+                                (t) == TREE_TYPE_MUSIC_DIR ?           \
+                                &artwork.mus_first :                   \
+                                NULL)
+
 // values for artwork handling
 #define LEVELDIR_ARTWORK_SET_PTR(leveldir, type)                       \
                                ((type) == ARTWORK_TYPE_GRAPHICS ?      \