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 class InitialInstancesContainer;
36 struct VariablesRenamingChangesetNode;
37 } // namespace gd
38 
39 namespace gd {
40 
42  std::unordered_map<gd::String, gd::String> oldToNewVariableNames;
43  std::unordered_map<gd::String, std::shared_ptr<gd::VariablesRenamingChangesetNode>>
44  modifiedVariables;
45 };
46 
48  std::unordered_set<gd::String> removedVariableNames;
49 
55  std::unordered_set<gd::String> typeChangedVariableNames;
56  std::unordered_set<gd::String> valueChangedVariableNames;
57  std::unordered_set<gd::String> addedVariableNames;
58 
59  bool HasRemovedVariables() { return !removedVariableNames.empty(); }
60 
61  VariablesChangeset& ClearRemovedVariables() { removedVariableNames.clear(); return *this; }
62 };
63 
72 class GD_CORE_API WholeProjectRefactorer {
73  public:
74 
78  static VariablesChangeset ComputeChangesetForVariablesContainer(
79  const gd::SerializerElement &oldSerializedVariablesContainer,
80  const gd::VariablesContainer &newVariablesContainer);
81 
86  static void ApplyRefactoringForVariablesContainer(
87  gd::Project &project, gd::VariablesContainer &variablesContainer,
88  const gd::VariablesChangeset &changeset,
89  const gd::SerializerElement &originalSerializedVariables);
90 
95  static void ApplyRefactoringForObjectVariablesContainer(
96  gd::Project &project, gd::VariablesContainer &objectVariablesContainer,
97  gd::InitialInstancesContainer &initialInstancesContainer,
98  const gd::String &objectName, const gd::VariablesChangeset &changeset,
99  const gd::SerializerElement &originalSerializedVariables);
100 
105  static void ApplyRefactoringForGroupVariablesContainer(
106  gd::Project &project, gd::ObjectsContainer &globalObjectsContainer,
107  gd::ObjectsContainer &objectsContainer,
108  gd::InitialInstancesContainer &initialInstancesContainer,
109  const gd::VariablesContainer &groupVariablesContainer,
110  const gd::ObjectGroup &objectGroup,
111  const gd::VariablesChangeset &changeset,
112  const gd::SerializerElement &originalSerializedVariables);
113 
122  static void RenameEventsFunctionsExtension(
123  gd::Project& project,
124  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
125  const gd::String& oldName,
126  const gd::String& newName);
127 
132  static void UpdateExtensionNameInEventsBasedBehavior(
133  gd::Project &project,
134  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
135  gd::EventsBasedBehavior &eventsBasedBehavior,
136  const gd::String &sourceExtensionName);
137 
142  static void UpdateExtensionNameInEventsBasedObject(
143  gd::Project &project,
144  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
145  gd::EventsBasedObject &eventsBasedObject,
146  const gd::String &sourceExtensionName);
147 
155  static void RenameEventsFunction(
156  gd::Project& project,
157  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
158  const gd::String& oldFunctionName,
159  const gd::String& newFunctionName);
160 
169  static void RenameBehaviorEventsFunction(
170  gd::Project& project,
171  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
172  const gd::EventsBasedBehavior& eventsBasedBehavior,
173  const gd::String& oldFunctionName,
174  const gd::String& newFunctionName);
175 
184  static void RenameObjectEventsFunction(
185  gd::Project& project,
186  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
187  const gd::EventsBasedObject& eventsBasedObject,
188  const gd::String& oldFunctionName,
189  const gd::String& newFunctionName);
190 
198  static void
199  RenameParameter(gd::Project &project,
200  gd::ProjectScopedContainers &projectScopedContainers,
201  gd::EventsFunction &eventsFunction,
202  const gd::ObjectsContainer &parameterObjectsContainer,
203  const gd::String &oldParameterName,
204  const gd::String &newParameterName);
205 
209  static void
210  ChangeParameterType(gd::Project &project,
211  gd::ProjectScopedContainers &projectScopedContainers,
212  gd::EventsFunction &eventsFunction,
213  const gd::ObjectsContainer &parameterObjectsContainer,
214  const gd::String &parameterName);
215 
224  static void MoveEventsFunctionParameter(
225  gd::Project& project,
226  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
227  const gd::String& functionName,
228  std::size_t oldIndex,
229  std::size_t newIndex);
230 
239  static void MoveBehaviorEventsFunctionParameter(
240  gd::Project& project,
241  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
242  const gd::EventsBasedBehavior& eventsBasedBehavior,
243  const gd::String& functionName,
244  std::size_t oldIndex,
245  std::size_t newIndex);
246 
255  static void MoveObjectEventsFunctionParameter(
256  gd::Project& project,
257  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
258  const gd::EventsBasedObject& eventsBasedObject,
259  const gd::String& functionName,
260  std::size_t oldIndex,
261  std::size_t newIndex);
262 
271  static void RenameEventsBasedBehaviorProperty(
272  gd::Project& project,
273  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
274  const gd::EventsBasedBehavior& eventsBasedBehavior,
275  const gd::String& oldPropertyName,
276  const gd::String& newPropertyName);
277 
286  static void RenameEventsBasedBehaviorSharedProperty(
287  gd::Project& project,
288  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
289  const gd::EventsBasedBehavior& eventsBasedBehavior,
290  const gd::String& oldPropertyName,
291  const gd::String& newPropertyName);
292 
301  static void RenameEventsBasedObjectProperty(
302  gd::Project& project,
303  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
304  const gd::EventsBasedObject& eventsBasedObject,
305  const gd::String& oldPropertyName,
306  const gd::String& newPropertyName);
307 
312  static void ChangeEventsBasedBehaviorPropertyType(
313  gd::Project &project,
314  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
315  const gd::EventsBasedBehavior &eventsBasedBehavior,
316  const gd::String &propertyName);
317 
322  static void ChangeEventsBasedObjectPropertyType(
323  gd::Project &project,
324  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
325  const gd::EventsBasedObject &eventsBasedObject,
326  const gd::String &propertyName);
327 
332  static void AddBehaviorAndRequiredBehaviors(gd::Project& project,
333  gd::Object& object,
334  const gd::String& behaviorType,
335  const gd::String& behaviorName);
340  static void AddRequiredBehaviorsFor(gd::Project& project,
341  gd::Object& object,
342  const gd::String& behaviorName);
343 
348  static std::vector<gd::String> FindDependentBehaviorNames(
349  const gd::Project& project,
350  const gd::Object& object,
351  const gd::String& behaviorName);
352 
356  static std::vector<gd::String> GetBehaviorsWithType(const gd::Object& object,
357  const gd::String& type);
358 
363  static std::vector<gd::UnfilledRequiredBehaviorPropertyProblem>
364  FindInvalidRequiredBehaviorProperties(const gd::Project& project);
365 
371  static bool
372  FixInvalidRequiredBehaviorProperties(gd::Project& project);
373 
381  static void RenameEventsBasedBehavior(
382  gd::Project& project,
383  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
384  const gd::String& oldBehaviorName,
385  const gd::String& newBehaviorName);
386 
391  static void UpdateBehaviorNameInEventsBasedBehavior(
392  gd::Project &project,
393  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
394  gd::EventsBasedBehavior &eventsBasedBehavior,
395  const gd::String &sourceBehaviorName);
396 
404  static void RenameEventsBasedObject(
405  gd::Project& project,
406  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
407  const gd::String& oldObjectName,
408  const gd::String& newObjectName);
409 
414  static void UpdateObjectNameInEventsBasedObject(
415  gd::Project &project,
416  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
417  gd::EventsBasedObject &eventsBasedObject,
418  const gd::String &sourceObjectName);
419 
423  static void RenameLayout(gd::Project &project, const gd::String &oldName,
424  const gd::String &newName);
428  static void RenameExternalLayout(gd::Project &project,
429  const gd::String &oldName,
430  const gd::String &newName);
434  static void RenameExternalEvents(gd::Project &project,
435  const gd::String &oldName,
436  const gd::String &newName);
437 
441  static void RenameLayerInScene(gd::Project &project, gd::Layout &scene,
442  const gd::String &oldName,
443  const gd::String &newName);
444 
448  static void RenameLayerInEventsBasedObject(
449  gd::Project &project,
450  gd::EventsFunctionsExtension &eventsFunctionsExtension,
451  gd::EventsBasedObject &eventsBasedObject, const gd::String &oldName,
452  const gd::String &newName);
453 
457  static void RenameLayerEffectInScene(gd::Project &project, gd::Layout &scene,
458  gd::Layer &layer,
459  const gd::String &oldName,
460  const gd::String &newName);
461 
465  static void RenameLayerEffectInEventsBasedObject(
466  gd::Project &project,
467  gd::EventsFunctionsExtension &eventsFunctionsExtension,
468  gd::EventsBasedObject &eventsBasedObject, gd::Layer &layer,
469  const gd::String &oldName, const gd::String &newName);
470 
474  static void RenameObjectAnimationInScene(gd::Project &project,
475  gd::Layout &scene,
476  gd::Object &object,
477  const gd::String &oldName,
478  const gd::String &newName);
479 
483  static void RenameObjectAnimationInEventsBasedObject(
484  gd::Project &project,
485  gd::EventsFunctionsExtension &eventsFunctionsExtension,
486  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
487  const gd::String &oldName, const gd::String &newName);
488 
492  static void RenameObjectPointInScene(gd::Project &project, gd::Layout &scene,
493  gd::Object &object,
494  const gd::String &oldName,
495  const gd::String &newName);
496 
500  static void RenameObjectPointInEventsBasedObject(
501  gd::Project &project,
502  gd::EventsFunctionsExtension &eventsFunctionsExtension,
503  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
504  const gd::String &oldName, const gd::String &newName);
505 
509  static void RenameObjectEffectInScene(gd::Project &project, gd::Layout &scene,
510  gd::Object &object,
511  const gd::String &oldName,
512  const gd::String &newName);
513 
517  static void RenameObjectEffectInEventsBasedObject(
518  gd::Project &project,
519  gd::EventsFunctionsExtension &eventsFunctionsExtension,
520  gd::EventsBasedObject &eventsBasedObject, gd::Object &object,
521  const gd::String &oldName, const gd::String &newName);
522 
529  static void ObjectOrGroupRenamedInScene(gd::Project &project,
530  gd::Layout &scene,
531  const gd::String &oldName,
532  const gd::String &newName,
533  bool isObjectGroup);
534 
541  static void ObjectRemovedInScene(gd::Project &project, gd::Layout &scene,
542  const gd::String &objectName);
543 
552  static void BehaviorsAddedToObjectInScene(gd::Project &project,
553  gd::Layout &layout,
554  const gd::String &objectName);
555 
562  static void ObjectRemovedInEventsBasedObject(
563  gd::Project& project,
564  gd::EventsBasedObject& eventsBasedObject,
565  const gd::String& objectName);
566 
572  static void ObjectOrGroupRenamedInEventsBasedObject(
573  gd::Project& project,
574  const gd::ProjectScopedContainers &projectScopedContainers,
575  gd::EventsBasedObject& eventsBasedObject,
576  const gd::String& oldName,
577  const gd::String& newName,
578  bool isObjectGroup);
579 
585  static void ObjectOrGroupRenamedInEventsFunction(
586  gd::Project& project,
587  const gd::ProjectScopedContainers &projectScopedContainers,
588  gd::EventsFunction& eventsFunction,
589  const gd::ObjectsContainer &targetedObjectsContainer,
590  const gd::String& oldName,
591  const gd::String& newName,
592  bool isObjectGroup);
593 
599  static void ObjectRemovedInEventsFunction(
600  gd::Project& project,
601  gd::EventsFunction& eventsFunction,
602  const gd::String& objectName);
603 
610  static void GlobalObjectOrGroupRenamed(gd::Project& project,
611  const gd::String& oldName,
612  const gd::String& newName,
613  bool isObjectGroup);
614 
621  static void GlobalObjectRemoved(gd::Project& project,
622  const gd::String& objectName);
623 
631  void BehaviorsAddedToGlobalObject(gd::Project &project,
632  const gd::String &objectName);
633 
638  static std::set<gd::String> GetAllObjectTypesUsingEventsBasedBehavior(
639  const gd::Project& project,
640  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
641  const gd::EventsBasedBehavior& eventsBasedBehavior);
642 
648  static void EnsureBehaviorEventsFunctionsProperParameters(
649  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
650  const gd::EventsBasedBehavior& eventsBasedBehavior);
651 
656  static void EnsureObjectEventsFunctionsProperParameters(
657  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
658  const gd::EventsBasedObject& eventsBasedObject);
659 
663  static void RemoveLayerInScene(gd::Project &project, gd::Layout &scene,
664  const gd::String &layerName);
665 
669  static void MergeLayersInScene(gd::Project &project, gd::Layout &scene,
670  const gd::String &originLayerName,
671  const gd::String &targetLayerName);
672 
676  static void
677  RemoveLayerInEventsBasedObject(gd::EventsBasedObject &eventsBasedObject,
678  const gd::String &layerName);
679 
683  static void
684  MergeLayersInEventsBasedObject(gd::EventsBasedObject &eventsBasedObject,
685  const gd::String &originLayerName,
686  const gd::String &targetLayerName);
687 
691  static void
692  RenameLeaderboards(gd::Project &project,
693  const std::map<gd::String, gd::String> &leaderboardIdMap);
694 
698  static std::set<gd::String> FindAllLeaderboardIds(gd::Project &project);
699 
704  static size_t GetLayoutAndExternalLayoutLayerInstancesCount(
705  gd::Project &project, gd::Layout &layout, const gd::String &layerName);
706 
707  virtual ~WholeProjectRefactorer(){};
708 
709  private:
710  static void ObjectOrGroupRenamedInScene(gd::Project &project,
711  gd::Layout &scene,
712  const gd::ObjectsContainer &targetedObjectsContainer,
713  const gd::String &oldName,
714  const gd::String &newName,
715  bool isObjectGroup);
716  static std::vector<gd::String> GetAssociatedExternalLayouts(
717  gd::Project& project, gd::Layout& layout);
718  static std::vector<gd::String>
719  GetAssociatedExternalLayouts(gd::Project &project,
720  const gd::String &layoutName);
721  static std::vector<gd::String>
722  GetAssociatedExternalEvents(gd::Project &project,
723  const gd::String &layoutName);
724 
725  static void DoRenameEventsFunction(gd::Project& project,
726  const gd::EventsFunction& eventsFunction,
727  const gd::String& oldFullType,
728  const gd::String& newFullType,
729  const gd::ProjectBrowser& projectBrowser);
730 
731  static void DoRenameBehavior(gd::Project& project,
732  const gd::String& oldBehaviorType,
733  const gd::String& newBehaviorType,
734  const gd::ProjectBrowser& projectBrowser);
735 
736  static void DoRenameObject(gd::Project& project,
737  const gd::String& oldObjectType,
738  const gd::String& newObjectType,
739  const gd::ProjectBrowser& projectBrowser);
740 
749  static void RenameEventsFunctionsExtension(
750  gd::Project& project,
751  const gd::EventsFunctionsExtension& eventsFunctionsExtension,
752  const gd::String& oldName,
753  const gd::String& newName,
754  const gd::ProjectBrowser& projectBrowser);
755 
763  static void RenameEventsBasedBehavior(
764  gd::Project &project,
765  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
766  const gd::EventsBasedBehavior &eventsBasedBehavior,
767  const gd::String &oldBehaviorName,
768  const gd::String &newBehaviorName,
769  const gd::ProjectBrowser &projectBrowser);
770 
778  static void RenameEventsBasedObject(
779  gd::Project &project,
780  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
781  const gd::EventsBasedObject &eventsBasedObject,
782  const gd::String &oldObjectName, const gd::String &newObjectName,
783  const gd::ProjectBrowser &projectBrowser);
784 
785  static void FindDependentBehaviorNames(
786  const gd::Project& project,
787  const gd::Object& object,
788  const gd::String& behaviorName,
789  std::unordered_set<gd::String>& dependentBehaviorNames);
790 
791  static std::shared_ptr<VariablesRenamingChangesetNode>
792  ComputeChangesetForVariable(const gd::Variable &oldVariable,
793  const gd::Variable &newVariable);
794 
795  static bool HasAnyVariableTypeChanged(const gd::Variable &oldVariable,
796  const gd::Variable &newVariable);
797 
798  static const gd::String behaviorObjectParameterName;
799  static const gd::String parentObjectParameterName;
800 
802 };
803 
804 } // 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
Defines a container of gd::InitialInstances.
Definition: InitialInstancesContainer.h:38
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:72
Definition: CommonTools.h:24
Definition: WholeProjectRefactorer.h:47
std::unordered_set< gd::String > typeChangedVariableNames
Definition: WholeProjectRefactorer.h:55
Definition: WholeProjectRefactorer.h:41