GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
WholeProjectRefactorer.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2016 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #pragma once
7 
8 #include <set>
9 #include <unordered_set>
10 #include <unordered_map>
11 #include <vector>
12 #include <memory>
13 #include "GDCore/String.h"
14 #include "GDCore/Project/Variable.h"
15 
16 namespace gd {
17 class Platform;
18 class Project;
19 class Layout;
20 class Layer;
21 class Object;
22 class ObjectGroup;
23 class String;
24 class EventsFunctionsExtension;
25 class EventsFunction;
26 class ObjectsContainer;
27 class VariablesContainer;
28 class EventsBasedBehavior;
29 class EventsBasedObject;
30 class Behavior;
31 class UnfilledRequiredBehaviorPropertyProblem;
32 class ProjectBrowser;
33 class SerializerElement;
34 class ProjectScopedContainers;
35 struct VariablesRenamingChangesetNode;
36 } // namespace gd
37 
38 namespace gd {
39 
41  std::unordered_map<gd::String, gd::String> oldToNewVariableNames;
42  std::unordered_map<gd::String, std::shared_ptr<gd::VariablesRenamingChangesetNode>>
43  modifiedVariables;
44 };
45 
47  std::unordered_set<gd::String> removedVariableNames;
48 
54  std::unordered_set<gd::String> typeChangedVariableNames;
55  std::unordered_set<gd::String> valueChangedVariableNames;
56  std::unordered_set<gd::String> addedVariableNames;
57 
58  bool HasRemovedVariables() { return !removedVariableNames.empty(); }
59 
60  VariablesChangeset& ClearRemovedVariables() { removedVariableNames.clear(); return *this; }
61 };
62 
71 class GD_CORE_API WholeProjectRefactorer {
72  public:
73 
77  static VariablesChangeset ComputeChangesetForVariablesContainer(
78  const gd::SerializerElement &oldSerializedVariablesContainer,
79  const gd::VariablesContainer &newVariablesContainer);
80 
85  static void ApplyRefactoringForVariablesContainer(
86  gd::Project &project, gd::VariablesContainer &variablesContainer,
87  const gd::VariablesChangeset &changeset,
88  const gd::SerializerElement &originalSerializedVariables);
89 
94  static void ApplyRefactoringForGroupVariablesContainer(
95  gd::Project &project, gd::ObjectsContainer &globalObjectsContainer,
96  gd::ObjectsContainer &objectsContainer,
97  const gd::VariablesContainer &groupVariablesContainer,
98  const gd::ObjectGroup &objectGroup,
99  const gd::VariablesChangeset &changeset,
100  const gd::SerializerElement &originalSerializedVariables);
101 
110  static void RenameEventsFunctionsExtension(
111  gd::Project& project,
112  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
113  const gd::String& oldName,
114  const gd::String& newName);
115 
120  static void UpdateExtensionNameInEventsBasedBehavior(
121  gd::Project &project,
122  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
123  gd::EventsBasedBehavior &eventsBasedBehavior,
124  const gd::String &sourceExtensionName);
125 
130  static void UpdateExtensionNameInEventsBasedObject(
131  gd::Project &project,
132  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
133  gd::EventsBasedObject &eventsBasedObject,
134  const gd::String &sourceExtensionName);
135 
143  static void RenameEventsFunction(
144  gd::Project& project,
145  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
146  const gd::String& oldFunctionName,
147  const gd::String& newFunctionName);
148 
157  static void RenameBehaviorEventsFunction(
158  gd::Project& project,
159  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
160  const gd::EventsBasedBehavior& eventsBasedBehavior,
161  const gd::String& oldFunctionName,
162  const gd::String& newFunctionName);
163 
172  static void RenameObjectEventsFunction(
173  gd::Project& project,
174  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
175  const gd::EventsBasedObject& eventsBasedObject,
176  const gd::String& oldFunctionName,
177  const gd::String& newFunctionName);
178 
186  static void
187  RenameParameter(gd::Project &project,
188  gd::ProjectScopedContainers &projectScopedContainers,
189  gd::EventsFunction &eventsFunction,
190  const gd::ObjectsContainer &parameterObjectsContainer,
191  const gd::String &oldParameterName,
192  const gd::String &newParameterName);
193 
197  static void
198  ChangeParameterType(gd::Project &project,
199  gd::ProjectScopedContainers &projectScopedContainers,
200  gd::EventsFunction &eventsFunction,
201  const gd::ObjectsContainer &parameterObjectsContainer,
202  const gd::String &parameterName);
203 
212  static void MoveEventsFunctionParameter(
213  gd::Project& project,
214  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
215  const gd::String& functionName,
216  std::size_t oldIndex,
217  std::size_t newIndex);
218 
227  static void MoveBehaviorEventsFunctionParameter(
228  gd::Project& project,
229  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
230  const gd::EventsBasedBehavior& eventsBasedBehavior,
231  const gd::String& functionName,
232  std::size_t oldIndex,
233  std::size_t newIndex);
234 
243  static void MoveObjectEventsFunctionParameter(
244  gd::Project& project,
245  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
246  const gd::EventsBasedObject& eventsBasedObject,
247  const gd::String& functionName,
248  std::size_t oldIndex,
249  std::size_t newIndex);
250 
259  static void RenameEventsBasedBehaviorProperty(
260  gd::Project& project,
261  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
262  const gd::EventsBasedBehavior& eventsBasedBehavior,
263  const gd::String& oldPropertyName,
264  const gd::String& newPropertyName);
265 
274  static void RenameEventsBasedBehaviorSharedProperty(
275  gd::Project& project,
276  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
277  const gd::EventsBasedBehavior& eventsBasedBehavior,
278  const gd::String& oldPropertyName,
279  const gd::String& newPropertyName);
280 
289  static void RenameEventsBasedObjectProperty(
290  gd::Project& project,
291  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
292  const gd::EventsBasedObject& eventsBasedObject,
293  const gd::String& oldPropertyName,
294  const gd::String& newPropertyName);
295 
300  static void ChangeEventsBasedBehaviorPropertyType(
301  gd::Project &project,
302  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
303  const gd::EventsBasedBehavior &eventsBasedBehavior,
304  const gd::String &propertyName);
305 
310  static void ChangeEventsBasedObjectPropertyType(
311  gd::Project &project,
312  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
313  const gd::EventsBasedObject &eventsBasedObject,
314  const gd::String &propertyName);
315 
320  static void AddBehaviorAndRequiredBehaviors(gd::Project& project,
321  gd::Object& object,
322  const gd::String& behaviorType,
323  const gd::String& behaviorName);
328  static void AddRequiredBehaviorsFor(gd::Project& project,
329  gd::Object& object,
330  const gd::String& behaviorName);
331 
336  static std::vector<gd::String> FindDependentBehaviorNames(
337  const gd::Project& project,
338  const gd::Object& object,
339  const gd::String& behaviorName);
340 
344  static std::vector<gd::String> GetBehaviorsWithType(const gd::Object& object,
345  const gd::String& type);
346 
351  static std::vector<gd::UnfilledRequiredBehaviorPropertyProblem>
352  FindInvalidRequiredBehaviorProperties(const gd::Project& project);
353 
359  static bool
360  FixInvalidRequiredBehaviorProperties(gd::Project& project);
361 
369  static void RenameEventsBasedBehavior(
370  gd::Project& project,
371  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
372  const gd::String& oldBehaviorName,
373  const gd::String& newBehaviorName);
374 
379  static void UpdateBehaviorNameInEventsBasedBehavior(
380  gd::Project &project,
381  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
382  gd::EventsBasedBehavior &eventsBasedBehavior,
383  const gd::String &sourceBehaviorName);
384 
392  static void RenameEventsBasedObject(
393  gd::Project& project,
394  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
395  const gd::String& oldObjectName,
396  const gd::String& newObjectName);
397 
402  static void UpdateObjectNameInEventsBasedObject(
403  gd::Project &project,
404  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
405  gd::EventsBasedObject &eventsBasedObject,
406  const gd::String &sourceObjectName);
407 
411  static void RenameLayout(gd::Project &project, const gd::String &oldName,
412  const gd::String &newName);
416  static void RenameExternalLayout(gd::Project &project,
417  const gd::String &oldName,
418  const gd::String &newName);
422  static void RenameExternalEvents(gd::Project &project,
423  const gd::String &oldName,
424  const gd::String &newName);
425 
429  static void RenameLayerInScene(gd::Project &project, gd::Layout &scene,
430  const gd::String &oldName,
431  const gd::String &newName);
432 
436  static void RenameLayerInEventsBasedObject(
437  gd::Project &project,
438  gd::EventsFunctionsExtension &eventsFunctionsExtension,
439  gd::EventsBasedObject &eventsBasedObject, const gd::String &oldName,
440  const gd::String &newName);
441 
445  static void RenameLayerEffectInScene(gd::Project &project, gd::Layout &scene,
446  gd::Layer &layer,
447  const gd::String &oldName,
448  const gd::String &newName);
449 
453  static void RenameLayerEffectInEventsBasedObject(
454  gd::Project &project,
455  gd::EventsFunctionsExtension &eventsFunctionsExtension,
456  gd::EventsBasedObject &eventsBasedObject, gd::Layer &layer,
457  const gd::String &oldName, const gd::String &newName);
458 
462  static void RenameObjectAnimationInScene(gd::Project &project,
463  gd::Layout &scene,
464  gd::Object &object,
465  const gd::String &oldName,
466  const gd::String &newName);
467 
471  static void RenameObjectAnimationInEventsBasedObject(
472  gd::Project &project,
473  gd::EventsFunctionsExtension &eventsFunctionsExtension,
474  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
475  const gd::String &oldName, const gd::String &newName);
476 
480  static void RenameObjectPointInScene(gd::Project &project, gd::Layout &scene,
481  gd::Object &object,
482  const gd::String &oldName,
483  const gd::String &newName);
484 
488  static void RenameObjectPointInEventsBasedObject(
489  gd::Project &project,
490  gd::EventsFunctionsExtension &eventsFunctionsExtension,
491  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
492  const gd::String &oldName, const gd::String &newName);
493 
497  static void RenameObjectEffectInScene(gd::Project &project, gd::Layout &scene,
498  gd::Object &object,
499  const gd::String &oldName,
500  const gd::String &newName);
501 
505  static void RenameObjectEffectInEventsBasedObject(
506  gd::Project &project,
507  gd::EventsFunctionsExtension &eventsFunctionsExtension,
508  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
509  const gd::String &oldName, const gd::String &newName);
510 
517  static void ObjectOrGroupRenamedInScene(gd::Project &project,
518  gd::Layout &scene,
519  const gd::String &oldName,
520  const gd::String &newName,
521  bool isObjectGroup);
522 
529  static void ObjectRemovedInScene(gd::Project &project, gd::Layout &scene,
530  const gd::String &objectName);
531 
540  static void BehaviorsAddedToObjectInScene(gd::Project &project,
541  gd::Layout &layout,
542  const gd::String &objectName);
543 
550  static void ObjectRemovedInEventsBasedObject(
551  gd::Project& project,
552  gd::EventsBasedObject& eventsBasedObject,
553  const gd::String& objectName);
554 
560  static void ObjectOrGroupRenamedInEventsBasedObject(
561  gd::Project& project,
562  const gd::ProjectScopedContainers &projectScopedContainers,
563  gd::EventsBasedObject& eventsBasedObject,
564  const gd::String& oldName,
565  const gd::String& newName,
566  bool isObjectGroup);
567 
573  static void ObjectOrGroupRenamedInEventsFunction(
574  gd::Project& project,
575  const gd::ProjectScopedContainers &projectScopedContainers,
576  gd::EventsFunction& eventsFunction,
577  const gd::ObjectsContainer &targetedObjectsContainer,
578  const gd::String& oldName,
579  const gd::String& newName,
580  bool isObjectGroup);
581 
587  static void ObjectRemovedInEventsFunction(
588  gd::Project& project,
589  gd::EventsFunction& eventsFunction,
590  const gd::String& objectName);
591 
598  static void GlobalObjectOrGroupRenamed(gd::Project& project,
599  const gd::String& oldName,
600  const gd::String& newName,
601  bool isObjectGroup);
602 
609  static void GlobalObjectRemoved(gd::Project& project,
610  const gd::String& objectName);
611 
619  void BehaviorsAddedToGlobalObject(gd::Project &project,
620  const gd::String &objectName);
621 
626  static std::set<gd::String> GetAllObjectTypesUsingEventsBasedBehavior(
627  const gd::Project& project,
628  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
629  const gd::EventsBasedBehavior& eventsBasedBehavior);
630 
636  static void EnsureBehaviorEventsFunctionsProperParameters(
637  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
638  const gd::EventsBasedBehavior& eventsBasedBehavior);
639 
644  static void EnsureObjectEventsFunctionsProperParameters(
645  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
646  const gd::EventsBasedObject& eventsBasedObject);
647 
651  static void RemoveLayerInScene(gd::Project &project, gd::Layout &scene,
652  const gd::String &layerName);
653 
657  static void MergeLayersInScene(gd::Project &project, gd::Layout &scene,
658  const gd::String &originLayerName,
659  const gd::String &targetLayerName);
660 
664  static void
665  RemoveLayerInEventsBasedObject(gd::EventsBasedObject &eventsBasedObject,
666  const gd::String &layerName);
667 
671  static void
672  MergeLayersInEventsBasedObject(gd::EventsBasedObject &eventsBasedObject,
673  const gd::String &originLayerName,
674  const gd::String &targetLayerName);
675 
679  static void
680  RenameLeaderboards(gd::Project &project,
681  const std::map<gd::String, gd::String> &leaderboardIdMap);
682 
686  static std::set<gd::String> FindAllLeaderboardIds(gd::Project &project);
687 
692  static size_t GetLayoutAndExternalLayoutLayerInstancesCount(
693  gd::Project &project, gd::Layout &layout, const gd::String &layerName);
694 
695  virtual ~WholeProjectRefactorer(){};
696 
697  private:
698  static void ObjectOrGroupRenamedInScene(gd::Project &project,
699  gd::Layout &scene,
700  const gd::ObjectsContainer &targetedObjectsContainer,
701  const gd::String &oldName,
702  const gd::String &newName,
703  bool isObjectGroup);
704  static std::vector<gd::String> GetAssociatedExternalLayouts(
705  gd::Project& project, gd::Layout& layout);
706  static std::vector<gd::String>
707  GetAssociatedExternalLayouts(gd::Project &project,
708  const gd::String &layoutName);
709  static std::vector<gd::String>
710  GetAssociatedExternalEvents(gd::Project &project,
711  const gd::String &layoutName);
712 
713  static void DoRenameEventsFunction(gd::Project& project,
714  const gd::EventsFunction& eventsFunction,
715  const gd::String& oldFullType,
716  const gd::String& newFullType,
717  const gd::ProjectBrowser& projectBrowser);
718 
719  static void DoRenameBehavior(gd::Project& project,
720  const gd::String& oldBehaviorType,
721  const gd::String& newBehaviorType,
722  const gd::ProjectBrowser& projectBrowser);
723 
724  static void DoRenameObject(gd::Project& project,
725  const gd::String& oldObjectType,
726  const gd::String& newObjectType,
727  const gd::ProjectBrowser& projectBrowser);
728 
737  static void RenameEventsFunctionsExtension(
738  gd::Project& project,
739  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
740  const gd::String& oldName,
741  const gd::String& newName,
742  const gd::ProjectBrowser& projectBrowser);
743 
751  static void RenameEventsBasedBehavior(
752  gd::Project &project,
753  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
754  const gd::EventsBasedBehavior &eventsBasedBehavior,
755  const gd::String &oldBehaviorName,
756  const gd::String &newBehaviorName,
757  const gd::ProjectBrowser &projectBrowser);
758 
766  static void RenameEventsBasedObject(
767  gd::Project &project,
768  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
769  const gd::EventsBasedObject &eventsBasedObject,
770  const gd::String &oldObjectName, const gd::String &newObjectName,
771  const gd::ProjectBrowser &projectBrowser);
772 
773  static void FindDependentBehaviorNames(
774  const gd::Project& project,
775  const gd::Object& object,
776  const gd::String& behaviorName,
777  std::unordered_set<gd::String>& dependentBehaviorNames);
778 
779  static std::shared_ptr<VariablesRenamingChangesetNode>
780  ComputeChangesetForVariable(const gd::Variable &oldVariable,
781  const gd::Variable &newVariable);
782 
783  static bool HasAnyVariableTypeChanged(const gd::Variable &oldVariable,
784  const gd::Variable &newVariable);
785 
786  static const gd::String behaviorObjectParameterName;
787  static const gd::String parentObjectParameterName;
788 
790 };
791 
792 } // namespace gd
Represents a behavior that is implemented with events.
Definition: EventsBasedBehavior.h:31
Represents an object that is implemented with events.
Definition: EventsBasedObject.h:30
Events that can be generated as a stand-alone function, and used as a condition, action or expression...
Definition: EventsFunction.h:38
Hold a list of Events Functions (gd::EventsFunction) and Events Based Behaviors.
Definition: EventsFunctionsExtension.h:39
Represents a layer of a layout.
Definition: Layer.h:91
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:40
Represents an object group.
Definition: ObjectGroup.h:28
Represent an object of a platform.
Definition: Object.h:37
Used as a base class for classes that will own objects (see gd::Object).
Definition: ObjectsContainer.h:37
Expose a subset of the project to workers.
Definition: ProjectBrowser.h:29
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:50
Holds references to variables, objects, properties and other containers.
Definition: ProjectScopedContainers.h:34
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
String represents an UTF8 encoded string.
Definition: String.h:33
Defines a variable which can be used by an object, a layout or a project.
Definition: Variable.h:29
Class defining a container for gd::Variable.
Definition: VariablesContainer.h:28
Tool functions to do refactoring on the whole project after changes like deletion or renaming of an o...
Definition: WholeProjectRefactorer.h:71
Definition: CommonTools.h:24
Definition: WholeProjectRefactorer.h:46
std::unordered_set< gd::String > typeChangedVariableNames
Definition: WholeProjectRefactorer.h:54
Definition: WholeProjectRefactorer.h:40